You've probably heard of LINQ. But what is it and how do you use it to search and organize your own data structures? Find out here!
You've probably heard of LINQ. But what is it? LINQ is a new technology that's integrated into the newest versions of C# and VB.NET dealing with organizing your data structures in a manner similar to the way databases organize data. If you've worked with SQL, for example, and you have to put data in a collection class, for example, you might have found that it would be nice to be able to query that data just as you do with collections.
That's what LINQ lets you do. But it's not just an enhancement to the collection classes. Instead, it's built right into the language. LINQ stands for Language Integrated Query, which means the querying capabilities are built right into the language. You can do queries against pretty much any kind of data, not just collections. This is one of those language features that, after you've used it, you step back and say, "Why didn't I think of this years ago?"
ADVERTISEMENT
Let's try it out.
First things first: You need the new Visual Studio 2008 Beta
In order to program with LINQ, you need the latest and greatest C# or VB.NET. The best place to get these is the latest beta of Visual Studio 2008 (codenamed Orcas). You can find it here.
Alternatively, if you have don't want to install the whole Visual Studio 2008 Beta, you can instead just install the .NET 3.5 framework, which is also currently in beta and can be found here. (That's 3.5, though—not 3.0. The 3.0 framework relies on the compilers found in the 2.0 framework and doesn't in itself have its own compilers.) With the .NET 3.5 framework, you can compile C# and VB.NET code at the command line. This is relatively easy to do, but you need your own editor and need to write all the code from scratch without the help of project templates and what-not. So in this article, I'll use Visual Studio 2008 Beta.
A quick example to get going
Here's a quick example that demonstrates the very basis of LINQ programming. This is an entire C# program that you can compile and run.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LINQConsole1
{
class Program
{
public static void Linq1()
{
int[] numbers = { 5, 10, 15, 20, 25, 30, 35, 40 };
var myNums =
from n in numbers
where n > 25
select n;
foreach (var x in myNums)
{
Console.WriteLine(x);
}
}
static void Main(string[] args)
{
Linq1();
}
}
}
This code creates and initializes an array of integers. That's nothing new. But right after that is an expression that may look a bit odd even to seasoned programmers. In fact, it looks like an SQL query! The code is an expression that says to obtain all the numbers in the array that are greater than 25, and store the results in a variable. Then the code loops through the resulting set and prints out the individual elements. Here's the output:
30
35
40
This, as you can see, is a list of all the numbers in the array that are greater than 25.
Look at the expression; this one has three parts: A from clause, a where clause, and a select clause. The from clause says where to get the data. The where clause specifies a restriction on which members of the data that we want. The final clause grabs the data. (However, the select clause is actually a bit more complex. I'll touch on that shortly.)
Next, notice the data type of the myNums variable: There isn't one. Instead we're using a datatype called var, which means the compiler will figure out (at compile time) what type to make for the variable. When programming with LINQ this helps simplify matters. However, clearly myNums must have a data type. In fact, it's an internal type that implements IEnumerable<int>. So you could rewrite the declaration like so, you prefer:
IEnumerable<int> myNums =
from n in numbers
where n > 25
select n;
But frankly, often your best bet is to use the var keyword instead, because sometimes it's not immediately obvious what type of object the IEnumerable holds, unlike this caes, where it's obviously an int. You'll see what I mean when I talk more about the select clause.