LINQ: The Philosophy
Applies To: .NET Framework 3.0, LINQ
LINQ (Language INtegrated Query) it is unified programming model for any kind of data in a data-centric application. Be it Database, XML or Object, this uses same query language throughout. This query language or set operation is integrated in the .NET ecosystem as natural expression language. This concept is based on more declarative programming approach. This is to give instruction to the execution about what you want to be done but not too much about how you want it to be done. This gives immense flexibility to developers to think about the business requirements than to think about the implementation of their coding logic. Also it allows us write our own code if we wish to. So it is not just a readymade package. Everything is done in memory like hash join, expression tree building etc.
How it works:
Suppose you are writing code in LINQ like
var emp = from e in Employee
where e.Age == 30
Internally it translates to method call like
var emp = Employee.Where(e=> e.Age == 30).Select(e.Name)
This creates an expression tree for all OPERATORS like Select, SelectMany, Take, Skip, TakeWhile, SkipWhile, Where, OrderBy, OrderByDescending, ThenBy, Reverse, Distinct, Union, Intersect, Except, First, FirstDefault, Range, Repeat, Min, Max, Sum, Average, Count are name to few.
But this depends on the Object (here Employee) what it is implementing.
Mainly LINQ expression builder implements two main interfaces
IEnumerable<T>: Is the implementation of LINQ to XML (XLinq).
IQueryable<T>: Implementation is based on
- LINQ to Object
- LINQ to Sql (DLinq)
- LINQ to Entities (Entity Data Model)
- LINQ to DataSet
- ……….. (lot more in near future)
Depending on the type of implementation the LINQ query processor returns the object that holds the expression tree and figure out how to execute and deliver the optimized result set.
In case of database interaction it turns the expression into SQL statements and then fetches back the rows to turn them into the object (in this case “Employee”).
All of them are tightly checked by the compiler and compiler will make sure of syntax etc.
The most interesting part is that we can implement IQueryable<T> over IEnumerable<T>. What it will do is basically create the expression dynamically at runtime and represent the result in IEnumerable<T>. During the execution it returns the IL and executes the result in form of object.
The best example of this is DataSet representation on LINQ. DataSet is in memory representation of data and not SQL database (or no database engine). But the inline architecture works like SQL database. You have index, primary key, relationship etc are available there. Even you can use small filter option in DataSet. It will effectively will implement IEnumerable<T> and will return value to object.
That’s how the LINQ works as per my understanding.
Took help from many BLOGS and articles and I cannot remember all of them now. But I must request you to visit this Channel 9 video of Anders Hejlsberg and Sam Druker.