How does LINQ work?

Before we conclude this chapter, we would like to give you a rough idea about how Language Integrated Query (LINQ) works under the hood, in a schematic manner. As we know, LINQ is a declarative language embedded inside a multi-paradigm language. The primary advantage of LINQ is the alignment to the rich type system of C#. Syntactically, LINQ is very similar to SQL language and the evaluation model is very similar to an SQL engine. As an example, let us explore a LINQ query which retrieves information regarding a set of employees by querying Employee and Department table. The query returns an anonymous type consisting of employee name, department name and location of the employee. We are using the comprehension syntax in this particular example:

    var empInfo = from emp in db.Employee
    join dept in db.Department
    on emp.deptid equals dept.nid
    select new
    {
      emp.Name,
      dept.Name,
      emp.Location
    };

While evaluating this LINQ statement, even though documentation states about outer sequence and inner sequence, schematically speaking, a cartesian product (aka cross join in database parlance) will be performed between Employee and Department table. The resulting data set will be filtered based on the join clause (on emp.deptid equals dept.nid), resulting in yet another data set.

Then, a project operation will be performed (select new { <data> }) to create an instance of new anonymous type, to add into a collection. The anonymous type will be synthesized by the C# compiler during the compile time. The above example uses comprehension style syntax and it will be transformed into a lambda expression syntax by the C# compiler, before generating the code. When we evaluate comprehension queries or mixed mode queries, the C# compiler transforms everything to lambda syntax before generating the code. The core algorithm for evaluation of LINQ queries are:

  • Cartesian product (of data sets involved)
  • Restrict or filter (where predicate)
  • Order by
  • Group operations
  • Projection (technical name for selecting subsets of fields, in a result)

To understand more about the query evaluation process, one can consult a book which deals with relational database theory, as this topics warrants another book! The LINQ was introduced with C# with version 3.0 of the language and the designers of language introduced the following features to the language to facilitate the implementation of LINQ. They are:

  • Lambda expressions and functions: To facilitate the passing of predicates and transformations as parameters to LINQ operator functions
  • Extension methods: To avoid the syntactic clutter while nesting LINQ operators (transformation of nested queries to fluent interface style)
  • Anonymous types: To allow developers to project the contents of a data set to types which are not declared ahead of time by the developers
  • Type inference: This feature was mandated because of the difficulty for programmers to identify the type of result from a LINQ operations

Try to understand in detail what has been covered in this section. If you are able to comprehend what has been dealt here tersely, it can help improve the competence as a developer.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.14.93