Five Things the Boss Should Know About Microsoft's LINQ

Do you think that LINQ is just another in a long series of odd Microsoft technologies? This database-meets-software-development technology could easily save your development staff time and avoid complexity!

By John Paul Mueller
Mon, June 16, 2008

CIO — You may not have heard about Microsoft's new brainchild, Language Integrated Query (LINQ). If you did hear about it, you may have yawned and put it down as yet another new technology searching for a requirement.

However, LINQ is different from most new technologies because it really can do everything it says it can. After working with LINQ for nearly a year, I've found that LINQ hasn't disappointed me, as have so many other technologies I've tried. LINQ is incredibly easy to use and quite reliable. The fact that I'm not alone in that sentiment is attested to by the large number of third-party add-ons created by other developers who feel the way I do about LINQ.

If you haven't encountered LINQ before, here's the straight-up definition. LINQ is a kind of query language, sort of the same as SQL, but the syntax is a little different. The basic idea is that you ask a question (or rather, your application does) and LINQ provides the answer from the data source you specify. That data source may not be a straight-up database. For example, if you ask LINQ which employees are in the accounting department and you supply Active Directory as the data source, the LINQ gets the answer for you from Active Directory.

Plenty has been written to explain LINQ to software developers. Here, I explain the important points for IT managers to understand.

1. Developers Can Access New Technologies Without Knowing Much About Them

A primary LINQ benefit is in reusability. A complex query you create to access objects also works with Active Directory, Microsoft SQL Server, MySQL, or a Web service; developers don't have to keep learning new techniques to use LINQ.

The basic LINQ setup comes with access for all of the data sources you might expect: data objects, SQL Server databases, XML and DataSets. However, if you stop there, you're missing LINQ's big picture. You can access a wealth of other data sources by obtaining a third-party provider—that is, LINQ add-ons, written by someone who isn't affiliated with Microsoft (and there are a ton of them). You can find common data sources, such as Active Directory, and uncommon data sources, such as Resource Description Framework (RDF).

To access data sources other than those the programming environment natively supports with most technologies, you have to jump through hoops or work through odd programming scenarios. If you make a change, a whole bunch of code usually has to change. With LINQ, you use the same query all the time. That simplifies the development process considerably, particularly for a programmer who's new to a project and hasn't made friends with the team's DBAs yet. Need a slightly techie example? Let's say, for the sake of simplicity, that you start with a C# string array like this:

String[] QueryString =
    { "One", "Two", "Three", "Four", "Five" };

To find all the strings that are more than three characters in length, you use a query like this:

var ThisQuery =
    from StringValue
    in QueryString
    where StringValue.Length > 3
    select StringValue;

In this case, ThisQuery is the output from the query process. You don't define an actual type because LINQ takes care of that for you; instead, you use var as the data type. The code uses StringValue to hold the individual data values from QueryString that match the where condition StringValue.Length > 3. The select portion of the query merely tells what to choose from the data source. Now, here's the really neat part. You can use that same query to retrieve data from an RDF or from Active Directory. If you have a provider for the data source, you can get to the data, using the same code. How cool is that?

Don't think that LINQ is limited to safe open-source projects or to Microsoft-specific data sources, either. LINQ works just fine with MySQL, as well as with a whole world of additional LINQ data sources.

You might assume that LINQ queries can't handle complex situations. The example in this section gives you a good idea where someone discovering LINQ will begin. However, LINQ queries, just like SQL queries, can become somewhat complex as you add data sources and output requirements. The learning curve to those complex queries is a gentle one, but even so, LINQ can become complex (like any other technology) as you ask it to do more work.

Continue Reading

Our Commenting Policies