Alex Rivera | Logout

How does Linq work (behind the scenes)?

Asked 2008-12-02T07:08:41.153
12

I was thinking about making something like Linq for Lua, and I have a general idea how Linq works, but was wondering if there was a good article or if someone could explain how C# makes Linq possible

Note: I mean behind the scenes, like how it generates code bindings and all that, not end user syntax.

Edit
Report

1 Answer

18

It's hard to answer the question because LINQ is so many different things. For instance, sticking to C#, the following things are involved:

  • Query expressions are "pre-processed" into "C# without query expressions" which is then compiled normally. The query expression part of the spec is really short - it's basically a mechanical translation which doesn't assume anything about the real meaning of the query, beyond "order by is translated into OrderBy/ThenBy/etc".
  • Delegates are used to represent arbitrary actions with a particular signature, as executable code.
  • Expression trees are used to represent the same thing, but as data (which can be examined and translated into a different form, e.g. SQL)
  • Lambda expressions are used to convert source code into either delegates or expression trees.
  • Extension methods are used by most LINQ providers to chain together static method calls. This allows a simple interface (e.g. IEnumerable<T>) to effectively gain a lot more power.
  • Anonymous types are used for projections - where you have some disparate collection of data, and you want bits of each of the aspects of that data, an anonymous type allows you to gather them together.
  • Implicitly typed local variables (var) are used primarily when working with anonymous types, to maintain a statically typed language where you may not be able to "speak" the name of the type explicitly.
  • Iterator blocks are usually used to implement in-process querying, e.g. for LINQ to Objects.
  • Type inference is used to make the whole thing a lot smoother - there are a lot of generic methods in LINQ, and without type inference it would be really painful.
  • Code generation is used to turn a model (e.g. DBML) into code
  • Partial types are used to provide extensibility to generated code
  • Attributes are used to provide metadata to LINQ providers

Obviously a

answered 2008-12-02T07:19:00.337

Your Answer