Development and Howto13 Aug 2007 08:50 am

I have just read about a new feature in Microsoft .Net 3.5 named LINQ. Microsoft describes this as

General-purpose query facilities added to the .NET Framework apply to all sources of information, not just relational or XML data. This facility is called .NET Language-Integrated Query (LINQ).

That, in my opinion, is a rather vague description of what this does. A query facility? Reading more, I see

We use the term language-integrated query to indicate that query is an integrated feature of the developer’s primary programming languages (for example, Visual C#, Visual Basic). Language-integrated query allows query expressions to benefit from the rich metadata, compile-time syntax checking, static typing and IntelliSense that was previously available only to imperative code. Language-integrated query also allows a single general purpose declarative query facility to be applied to all in-memory information, not just information from external sources.

This still doesn’t quite explain what this query facility with syntax checking and IntelliSense does. When I continued reading the page, I finally came across an example. Imagine the following C# code:

Using System;
using System.Linq;
using System.Collections.Generic;
class app {
static void Main() {
string[] names = { "Burke", "Connor", "Frank",
"Everett", "Albert", "George",
"Harris", "David" };
}
}

Now, imagine you wanted to get all the names that were 5 characters long, and print them in all upper case. What you would probably do is similar to the following: (PSEUDO CODE)

string upperNames[]
//get all names that are length 5 and push them onto the upperNames
for (int x=0;x if (names[x].length() == 5) {
upperNames.push(names[x].ToUpper());
Console.WriteLine(upperNames.peek());
}
}

Here is how you would accomplish the following with LINQ:

IEnumerable query = from s in names
where s.Length == 5
orderby s
select s.ToUpper();

Wow, that's some pretty cool stuff. There is some more stuff that is outlined in the article, but at a first glance, it's pretty neat.

One Response to “LINQ”

  1. on 13 Aug 2007 at 10:25 pm Luke Hoersten

    That is sexy. When I first started programming with Mono, I took a lot of slack from other Linux devs about using a MS product. As long as it’s free and can do stuff like this, I don’t care who invented it.

    IMHO, .NET is the coolest stuff coming out of MS right now.