Freigeben über


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

  • Anonymous
    September 13, 2005
    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

  • Anonymous
    September 13, 2005
    Well PDC is underway and for all the nice Vista, WinFX and Office 12 news, what really excites me most...

  • Anonymous
    September 13, 2005
    Well PDC is underway and for all the nice Vista, WinFX and Office 12 news, what really excites me most...

  • Anonymous
    September 13, 2005
    Well PDC is underway and for all the nice Vista, WinFX and Office 12 news, what really excites me most...

  • Anonymous
    September 13, 2005
    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.

  • Anonymous
    September 13, 2005
    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.

  • Anonymous
    September 14, 2005
    Dan Fernandez has blogged abouth the LINQ Project (Language INtegrated Query). LINQ makes query and set...

  • Anonymous
    September 16, 2005
    Language INtegrated Query. Asa a hotarat Microsoft sa isi numeasca noul proiect. Ce se incearca de fapt...

  • Anonymous
    September 17, 2005
    Language INtegrated Query. Asa a hotarat Microsoft sa isi numeasca noul proiect. Ce se incearca de fapt...

  • Anonymous
    September 17, 2005
    Will LINQ/DLINQ work with databases other than MS-SQL Server? I hope (and believe) the answer is yes, but how?

  • Anonymous
    September 18, 2005
    Blog link of the week 37

  • Anonymous
    September 18, 2005
    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!

  • Anonymous
    September 19, 2005
    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

  • Anonymous
    October 02, 2005
    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.

  • Anonymous
    January 22, 2006
    How about:

    aCollection select: [:c | c contactTitle length = 5]
    thenCollect [:x | x contactName].

  • Anonymous
    October 12, 2006
    PingBack from http://muhammadadel.wordpress.com/2006/02/14/linq/

  • Anonymous
    November 01, 2006
    PingBack from http://bothack.wordpress.com/2006/01/22/linq-framework/

  • Anonymous
    June 18, 2007
    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.

  • Anonymous
    February 03, 2010
    http://blogs.msdn.com/danielfe/archive/2005/09/13/464904.aspx