(Print this page)

My thoughts on LINQ
Published date: Tuesday, May 22, 2007
On: Moer and Éric Moreau's web site

At the time of writing this column, I am preparing myself for the DevTeach conference e that will be held in May in Montreal. I have to sessions there about LINQ. The first explains the theory and the second one will only be demos. Later this year, I will write a column (and probably more then one as it is a huge subject). But for now, I will just give you my very personal thoughts on that subject.

What is LINQ?

There is a lot of hype around LINQ. LINQ stands for Language Integrated Query. It is surely the biggest feature of the next release of Visual Studio (codename Orcas) that should be released early in 2008. In short, I would say that it offers a uniform approach to querying data wherever it comes from.

This feature is not something on which they just start working. In fact, Anders Hejlsberg has been working on this for the last 3 years. You don’t know Anders? You can think of him as the father of Delphi and C#. This is very convincing on a résumé. Anders once said: “LINQ will ultimately bind data querying into programming much as it was done decades ago with programs such as dBase and FoxPro”. So what that means? Programming languages like VB or C# are very oriented on how the tasks are being done (imperative programming) instead of what has to be done (declarative programming). So LINQ is really focus to ease data handling and by data not only mean relational data that is coming from a database but also objects data (like arrays and collections) and XML data.

To be able to handle the new LINQ feature, VB 9.0 and C# 3.0 (which will both run on the .Net Framework 3.5 – which is then next version), have been enhanced with some new features like Extension methods (possibility to add new methods to objects by extending a type rather then inheriting the type), Lambda expressions (a kind of inline functions), Type inference (as the ability to discover and create a new data type base on the return of an expression), Anonymous types (create a new type without defining it first).

The main advantages of LINQ are to provide syntax checking, schema checking, Intellisense, data access unification.

What are the main flavors of LINQ?

There are 3 main flavors of LINQ:

  • LINQ to Objects: all objects inheriting from the IEnumerable interface like arrays and collections can be used with LINQ.
  • LINQ to Data (formerly known as DLINQ): there are in fact 3 sub-flavors of LINQ to Data. The first one is LINQ to Dataset that let you use LINQ over an existing dataset that is already open no matter how you filled it. Secondly, LINQ to SQL which will query the database by its own mechanism. Currently, only Microsoft SQL server can be targeted but other vendors might give us their own provider. The last one is LINQ to Entities which is probably the first incursion of Visual Studio into the world of ORM (Object-Relational Mapping).
  • LINQ to XML (formerly known as XLINQ): to replace the need to use the classes available today that are not very friendly to handle XML data.

Figure 1: LINQ Overview

What does it look like?

LINQ queries have some similarities but also some differences with SQL queries. The biggest difference is that the query starts with the from clause instead of the select clause. This requirement is mainly related to the Intellisense and type checking.

Where can I get it?

At the time of writing, the first beta version of Orcas was just released (mid April). Microsoft made a really great job by providing Orcas already package as a virtual machine. It is a bit huge (about 4.5 gigs to download and 12 gigs to install) but this way you don’t have to do it yourself. To download this version, go to this place.

There are also a couple of versions (Professional, Team suite, Team Foundation Server) available. If you don’t like VPC or if you hardware won’t let you run it, there are also some downloadable bits that installs directly on your PC (but I do not recommend installing beta bits on production PC).

Finally, there is something that requires much fewer resources: the May 2006 CTP (Community Technology Preview) of LINQ. The download is about 16 MB (not GB). I just want you to be warned that this is almost 1 year old and some of the syntax and features has changed since that time. This downloads installs bits over VS 2005. This is available from this other place.

A short example

For those of you who never saw anything about LINQ, here is a short example that shows it.

Say you want to list all the processes that are in memory. You can very easily do it by querying the GetProcesses method of the Process component. You can do it today with a loop. Now say that you want to filter only for those processes that are using more then 10 mb of memory. If you would have to do it today, you could add a condition into your loop. Finally, say that you want sort those filtered processes by the name of there Process. To do this today, you would need to store the processes into a structure, then sort it before being able to display (or use) them.

LINQ let us do all this in a syntax that will look familiar to you:

Dim procQuery = From p In System.Diagnostics.Process.GetProcesses _
                Where p.WorkingSet64 > 10 * 1024 * 1024  _
                Order By p.ProcessName _
                Select p.ProcessName, p.WorkingSet64, p.Threads.Count, p.Responding
DataGridView1.DataSource = procQuery.ToList

I am almost sure that you can read it without any problems. You surely recognize the From, Where, Order By, and Select operators from the SQL language. But wait a minute! The Select clause is at the bottom! In order to have Intellisense working, you need to start your query with the From clause. In VB, all other operators can be in any order (but C# is stricter on this sequence).

By running this, you would see a DataGrid containing the 4 fields returned by the Select clause and filled with processes using more then 10 mb of memory sorted by the name of there processes.

Want to know more?

There is a lot of information about on the web. The bad thing is that a lot of sources are out-dated because the specifications changed over the time. Maybe one of the greatest sources of information is the LINQ Project page from Microsoft itself.

Conclusion

You will find it convenient to start using LINQ to loop through collections and process XML data. You might not jump right in to handle data. But on the long run, I am pretty sure that you will use it anywhere you can because it will standardize your coding and your thoughts.

I don’t even consider to have scratched the surface of this topic. Stay tuned and I will get back with a lot more details and examples.


(Print this page)