An Understanding of LINQ (Language-Integrated Query)

Language-Integrated Query (LINQ) is an innovation introduced in Visual Studio 2008 and .NET Framework version 3.5 that bridges the gap between the world of objects and the world of data.

LINQ extends powerful query capabilities to the language syntax of C# and Visual Basic. LINQ introduces standard, easily-learned patterns for querying and updating data, and the technology can be extended to support potentially any kind of data store. Visual Studio includes LINQ provider assemblies that enable the use of LINQ with .NET Framework collections, SQL Server databases, ADO.NET Datasets, and XML documents.

Hence, LINQ is independent of a data source and their database implementations, in other words SQL databases, XML documents, various Web services, and so on. Furthermore, LINQ supports full type checking and IntelliSense support so it can show any error during compile time rather than using a SQL Server database that will only show errors during runtime.

Walk Through

1. Create a New project - Console Application as shown below:

Create a New project

Console Application

2. Create Three Classes as illustrated below:

Create Three Classes

Explanation
 

Here we create a ClsCustomer class with constructor and the properties CustomerName, CustomerCode and CustomerCity and an Orders collection.

The Order property is mapped to the ClsOrder Class with a one-to-many relationship that consists of the ProductName property.

The third class is ClsLinq that contains the strongly typed collections of objects and public methods to return the queried data to our Main Program.

For example, objCustomers is defined as follows as a strongly typed collection of objects, for example even though we can implement any kind of data source to fill this datasource.       

private readonly ClsCustomer[] objCustomers =

{

    new ClsCustomer("001""Milson""Banepa",

    new[] {new ClsOrder("Surface"), new ClsOrder("Lenovo Yoga")}),

    new ClsCustomer("002""Nisha""Boise"new[] {new ClsOrder("Lays")})

};

Since the defined collections of objects support IEnumerable or the generic IEnumerable<T> interface, in the methods we have tried to return such collections to the main calling function.

generic IEnumerable interface

3. Query Syntax and Method Syntax

Query Syntax

Most queries in the introductory Language Integrated Query (LINQ) documentation is written using the LINQ declarative query syntax. However, the query syntax must be translated into method calls for the .NET common language runtime (CLR) when the code is compiled. These method calls invoke the standard query operators, that have names such as Where, Select, GroupBy, Join, Max, and Average. You can call them directly using method syntax instead of query syntax.

Basically, LINQ is in the System.Linq namespace.

//Query syntax:
 

IEnumerable<ClsCustomer> objCust =

       from obj1 in objCustomers

       where obj1.CustomerCode == "001"

       orderby

            obj1.CustomerName

      select obj1;

//Method syntax:
 

IEnumerable<ClsCustomer> obj2 =

       objCustomers.Where(clsCustomer => clsCustomer.CustomerCode == "001")

.OrderBy(n => n);

The output from the two examples is identical. The standard query operators are implemented as a new kind of method called extension methods. Extensions methods "extend" an existing type; they can be called as if they were instance methods on the type. The standard query operators extend IEnumerable<T> and that is why you can write objCustomers.Where(...).

Using Lambda Expressions in this example, we have seen an in-line argument to the Where method is:

.Where(clsCustomer => clsCustomer.CustomerCode == "001")

This inline expression is called a lambda expression. It is a convenient way to write code that would otherwise need to be written in a more cumbersome form as an anonymous method or a generic delegate or an expression tree. In C# "=>" is the lambda operator, that is read as "goes to". Thus the method syntax of LINQ uses lambda expressions that make the query simple and clean. Here, "Where" produces a filtered sequence, and then the "Orderby" operates on that sequence by sorting it.

For more details and explanations of how LINQ works, I suggest users to go through the attached download Zip so that it will be clearer and debug the project using breakpoints so that the workflow will be more clear. Finally, any comment is heartily welcome.

Up Next
    Ebook Download
    View all
    Learn
    View all