What is LINQ

LINQ is short for Language Integrated Query. If you are used to using SQL to query databases, you are going to have something of a head start with Linq, since they have many ideas in common.It is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages. Many of the concepts that LINQ has introduced were originally tested in Microsoft's Cω research project. LINQ was released as a part of .NET Framework 3.5 on November 19, 2007.

Imagine we have a list of orders. For this example, we will imagine they are stored in memory, but they could be in a file on disk too. We want to get a list of the costs of all orders that were placed by the customer identified by the number 78. If we set about implementing this in C# before version 3 and a range of other popular languages, we would probably write something like (assuming C# syntax for familiarity):

List Found = new List();
foreach (Order o in Orders)
    if (o.CustomerID == 78)
        Found.Add(o.Cost);

Here we are describing how to achieve the result we want by breaking the task into a series of instructions. This approach, which is very familiar to us, is called imperative programming. It relies on us to pick a good algorithm and not make any mistakes in the implementation of it; for more complex tasks, the algorithm is more complex and our chances of implementing it correctly decrease.

If we had the orders stored in a table in a database and we used SQL to query it, we would write something like:

SELECT Cost FROM Orders WHERE CustomerID = 78

Here we have not specified an algorithm, or how to get the data. We have just declared what we want and left the computer to work out how to do it. This is known as declarative or logic programming.

Linq brings declarative programming features into imperative languages. It is not language specific, and has been implemented in the newer versions of Dot Net languages.

      // Linq query.
        var Found = from o in Orders
                    where o.CustomerID == 78
                    select o.Cost;

from o in Orders

from is the keyword that always starts a query. You can read it a little bit like a "foreach": it takes a collection of some kind after the "in" keyword and makes what is to the left of the "in" keyword refer to a single element of the collection.

where o.CustomerID == 78

where this is another new keyword, this introduces a filter, allowing us to pick only some of the objects from the Orders collection. The "from" made the identifier "o" refer to a single item from the collection, and we write the condition in terms of this. If you type this query into the IDE yourself, you will notice that it has worked out that "o" is an Order and intellisense works as expected.

select o.Cost

select is the final new keyword, this comes at the end of the query and is a little like a "return" statement: it states what we want to appear in the collection holding the results of the query.

This looks like SQL but kind of backwards and twisted about a bit.The important thing to remember is that all of the conditions are to be expressed in C# syntax, not SQL syntax. That means "==" for equality testing, rather than "=" in SQL.

It is possible to sort the results based upon a field or the result of a computation involving one or more fields. This is achieved by using the new "orderby" keyword.

var Found = from o in Orders
            where o.CustomerID == 78
            orderby o.Cost descending
            select new { o.OrderID, o.Cost };

After the "orderby" keyword, we write the expression that the objects will be sorted on. In this case, it is a single field. Notice this is different from SQL, where there are two words: "ORDER BY". The result is that we now get the orders in order of decreasing cost. To get most expensive last, we would have used the "ascending" keyword, which is the default.

These videos are focused on the new LINQ language features included in Visual Studio 2008.

LINQ Video 1 LINQ Video 2
LINQ Video 3 LINQ Video 4
LINQ Video 5 LINQ Video 6
LINQ Video 7 LINQ Video 8
LINQ Video 9

Reference Articles