Arguably, LINQ (Language Integrated Query) will be the next major revolution in programming languages. LINQ is a language-independent, platform-independent technology that solves an old problem - data queries in your code- in a very elegant way. First, it brings a highly declarative approach in performing queries and data-bindings. Second, it is strongly typed at compile time. Third, it is easily extensible for any type of data stores (databases, XML documents, or anything else).

I personally view LINQ as the SQL language of the XXI century. I won't be surprised to see the same idea propagated in any modern strongly-typed programming language (for example Java).

To illustrate its full power, we will present a simple LINQ query, in C# 3.0:  

Northwind db = new Northwind("c:\\northwind\\northwnd.mdf");

var q =
      from c in db.Customers
      where c.City == "London"
      select c;

foreach (var cust in q)
      Console.WriteLine("id = {0}, City = {1}",cust.CustomerID, cust.City);

Everything you see here is strongly typed. All the data bindings (the Customer data structure to the Customer table) are done at compile time. Not only that the generated code is extremely fast, but the language integration is natural.

Under the cover, there is a lot going on. First of all, the "Customers" property above is inferred at compile time as being the Customers table from the Northwind database. The associated fields (CustomerID, City) are inferred as columns in the Customers database.

All the type assignments are done at compile time, including the type binding for every field. For example, don't be fooled by the "var q" declaration above. It is not a type-less variable like in JScript. Rather, the "q" variable has a well-defined type: a list/array of structures identifying query records, each of them being a well-defined structure, with strongly-typed fields (whose typeis inferred from the database or XML schema).

The same technology can be extended efficiently to do declarative data manipulation directly in the query. For example, the following code:

var q =
     
from c in db.Customers, o in db.Orders
     
where c.CustomerId == o.CustomerID && c.City == "London"
      select o;

will be used to construct under the covers a true SQL Join statement, binding the Customers and Orders tables. It's that simple!

I think that programmers that are doing database programming, or XML processing will love it.

 

More links on LINQ

- LINQ home page on MSDN. This is the most comprehensive page.
- LINQ project overview.  
- LINQ samples in C#.
- LINQ downloadable preview (compiler, samples, and more!) here.