Announced at PDC: The LINQ Project


The LINQ Project


On Tuesday September 13, 2005, Anders Hejlsberg did the first public demo of the LINQ Project in the Jim Allchin keynote.


 


What is LINQ you might ask?


LINQ stands for Language INtegrated Query and in a nutshell, it makes query and set operations, like SQL statements first class citizens in .NET languages like C# and VB. 


 


Query expressions, like “from”, “where”, and “select” and all the others you know and love from SQL are now first class citizens in C# and VB. Not only that, the query expressions can be used across domains. While in this example I’m querying objects, I could just as easily query a database as well.


 


What does LINQ code look like?


The sample below queries a list of strings and returns all strings with a length of five.


 


using System;


using System.Query;


using Danielfe;


 


class Program


{


    static void Main(string[] args)


    {


        string[] aBunchOfWords = {“One”,“Two”, “Hello”,


  “World”, “Four”, “Five”};


    


        var result =   


            from s in aBunchOfWords // query the string array


            where s.Length == 5     // for all words with length = 5


            select s;               // and return the string


       


        //PrintToConsole is an Extension method that prints the value


        result.Print();


    }


}


 


This application prints the following result


Hello


World


 


The beauty of LINQ is that you can use query operations on anything. Here’s a sample that runs against SQL Server 2000 on the Northwind database and returns all customers who’s title is five.


 


using System;


using System.Query;


using Danielfe;


using System.Data.DLinq; //DLinq is LINQ for Databases


using nwind; //Custom namespace that is tool generated


 


class Program


{


    static void Main(string[] args)


    {


        Northwind db = new Northwind(“Data Source=(local);Initial Catalog=Northwind;Integrated Security=True”);  


        Table<Customers> allCustomers = db.GetTable<Customers>();


 


        var result =


                from c in allCustomers


                where c.ContactTitle.Length == 5


                select c.ContactName;


 


        result.Print();  


    }


}


 


The Customers class is an auto-generated class that lets you program against the customer table. The first two lines establish a connection to the database and retrieve data for the Customer table. The second line queries all northwind customers who’s ContactTitle is five characters long and returns the ContactName and prints them to the console.


 


In short, LINQ makes it a heck of a lot easier to program any sort of data source in a unified way.  


 


More information:



 

Comments (32)

  1. chai si says:

    Hi,

    in case you have some influence on the design of linq, please consider these simplifications:

    1) make the select statement optional (use it only, if something nested is returned)

    2) offer implicit casts like List<Customer> = from db.Customers

    Kind regards,

    Chaii

  2. Well PDC is underway and for all the nice Vista, WinFX and Office 12 news, what really excites me most…

  3. Well PDC is underway and for all the nice Vista, WinFX and Office 12 news, what really excites me most…

  4. Well PDC is underway and for all the nice Vista, WinFX and Office 12 news, what really excites me most…

  5. Kevin Daly says:

    Very, very cool.

    I love things like this that actually help people to get things done.

    One thing I’m still a little unclear on: will we have to wait for C# 3.0 and VB9 to use LINQ in production code, or will something be made available as an add-on for C# 2.0 and VB8?

    I’m not even *at* PDC and I’m already suffering from the "I want it, and I want it NOW" bug.

  6. I LOVE THIS. I don’t know how many people are comfortable with SQL, but I was a database developer for years, and this will save LOTS of code.

  7. Dan Fernandez has blogged abouth the LINQ Project (Language INtegrated Query). LINQ makes query and set…

  8. Language INtegrated Query. Asa a hotarat Microsoft sa isi numeasca noul proiect. Ce se incearca de fapt…

  9. Language INtegrated Query. Asa a hotarat Microsoft sa isi numeasca noul proiect. Ce se incearca de fapt…

  10. Will LINQ/DLINQ work with databases other than MS-SQL Server? I hope (and believe) the answer is yes, but how?

  11. Daniel Moth says:

    Blog link of the week 37

  12. miki says:

    Whoa…another idea of doing the same thing in other way, so instead of

    "select from …" you put macro inside the language and what?…is it faster?

    I really don’t see – instead of 3 lines with SQL there are 5.

    Brilliant!

  13. Julian Hyde says:

    The Saffron project provides a very similar integration of relational concepts into the Java language.

    It’s an open source project based upon the OpenJava Java compiler, and incorporates a query optimizer to help make the right decision when handling heterogeneous queries (say joining a small RDBMS table to a large Java array).

    If you’re interested in working on this project, please contact me.

    Julian

  14. Marcel says:

    More language design by agglutination. Sigh.

    How about:

    aBunchOfWords := ( "One","Two", "Hello", "World", "Four", "Five" ).

    stdout do println: (aBunchOfWords selectWhere length = 5 ) each.

    Hello

    World

    That’s Higher Order Messaging, which works without extending the language.

  15. Alex says:

    How about:

    aCollection select: [:c | c contactTitle length = 5]

    thenCollect [:x | x contactName].

  16. Anonymous says:

    LINQ stands for Language INtegrated Query and in a nutshell, it makes query and set operations, like SQL statements first class citizens in .NET languages like C# and VB.