score:18

Accepted answer

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 lot of these aren't only used by linq, but different linq technologies will depend on them.

if you can give more indication of what aspects you're interested in, we may be able to provide more detail.

if you're interested in effectively implementing linq to objects, you might be interested in a talk i gave at ddd in reading a couple of weeks ago - basically implementing as much of linq to objects as possible in an hour. we were far from complete by the end of it, but it should give a pretty good idea of the kind of thing you need to do (and buffering/streaming, iterator blocks, query expression translation etc). the videos aren't up yet (and i haven't put the code up for download yet) but if you're interested, drop me a mail at skeet@pobox.com and i'll let you know when they're up. (i'll probably blog about it too.)

score:1

matt warren's blog has all the answers (and a sample iqueryable provider implementation to give you a headstart):

http://blogs.msdn.com/mattwar/

score:2

perhaps my linq for r6rs scheme will provide some insights.

it is 100% semantically, and almost 100% syntactically the same as linq, with the noted exception of additional sort parameters using 'then' instead of ','.

some rules/assumptions:

  • only dealing with lists, no query providers.
  • not lazy, but eager comprehension.
  • no static types, as scheme does not use them.

my implementation depends on a few core procedures:

  • map - used for 'select'
  • filter - used for 'where'
  • flatten - used for 'selectmany'
  • sort - a multi-key sorting procedure
  • groupby - for grouping constructs

the rest of the structure is all built up using a macro.

bindings are stored in a list that is tagged with bound identifiers to ensure hygiene. the binding are extracted and rebound locally where ever an expression occurs.

i did track the progress on my blog, that may provide some insight to possible issues.

score:2

for design ideas, take a look at c omega, the research project that birthed linq. linq is a more pragmatic or watered down version of c omega, depending on your perspective.

score:3

mono (partially?) implements linq, and is opensource. maybe you could look into their implementation?


Related Query

More Query from same tag