- At a compiler level, LINQ is a series of transformations done to expressions that begin with FROM and end with SELECT or GROUP BY. These transformations turn free text queries into extension methods. For example, var query = from A in aList select A.ToUpper() is transformed by the compiler into var query = aList.Select(a => a.ToUpper()). But, and this is a key point, the implementation of the methods is not determined. You determine the implementation by the namespaces you include; you could write your own implementation of Select, Where, etc.
- LINQ expressions provide access to local objects or external data sources. Although the same syntax is used, the implementation is fundamentally different.
- Local LINQ expressions are executed through a series of IEnumerator MoveNext actions, moving from right to left. For example, with an expression like aList.Where(...).Select(..).OrderBy(...), when the OrderBy receives a MoveNext command from calling code, and in turn issues a MoveNext to the Select method, and so on until the Where method calls MoveNext on the original collection enumerator.
- External data sources, such as SQL Server, receive the LINQ query as a single SQL query request. A series of OrderBy/ThenBy clauses, for example, are transmitted to the database as elements of a single ORDER BY clause.
- The query expression is implemented using the Decorator pattern, where steps in logic are applied over each other in successive layers. Each LINQ step has a reference to an IEnumerable collection and an expression to apply to the collection. with each step in the query containing a link to an IEnumable collection and the logic to apply to it, in a sequence that the Albaharis aptly compare to a set of Russian dolls.
Monday, March 19, 2012
Looking at LINQ
I've been digging through the discussion of LINQ in the Albahari brothers' C# 4.0 in a Nutshell: The Definitive Reference, and it has cleared up a number of points for me:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment