Possible Duplicate:
Learning about LINQ
Hi everyone,
I just want to understand what exactly is LINQ in DOTNET? And How does it work?
Tx
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
LINQ is many things, it’s the combination of many smaller things.
This answer is going to be a jumble of information, I apologize. Your best bet is to wait a bit and see if someone else summarizes it better, and do google for the keywords I use.
LINQ stands for “Language INtegrated Query”, and the most naive interpretation is that they added SQL-like syntax to the C# programming language.
So instead of:
they have the syntax:
The C# compiler will actually translate the second piece of code above to the first piece of code, so in reality, you’re just calling methods on the collections.
However, and here’s another part of the puzzle. Those methods are not actually defined in the collections at all. They’re defined as extension methods, which means they’re defined somewhere else, with some trickery that basically says “let the programmer use these methods as though they were defined in the collection type to begin with, and just fix the code during compilation”.
So the first piece of code above:
actually ends up being compiled as:
The Where method is defined here: Enumerable.Where.
Next piece of magic is that the C# compiler doesn’t use Enumerable.Where, what it does is that it just rewrites the code on the fly to look like the second piece of code in my answer here, and let the normal type inference work it out. In other words, it’s going to pretend you actually wrote the second piece of code, and then see that “otherValues” is a
List<T>whereTis anint, and then find thatEnumerable.Whereis the one to call.This means that you can, for other types than collections, actually make your own implementations of Where, and the LINQ syntax would be none the wiser.
This means … that things that aren’t really in-memory collections can be queried. For instance, if “otherValues” above is something that knows how to get data from a database, a different Where method will be called, not the one in Enumerable.Where.
This allows those other implementations to do their things in their own way, for instance by writing the SQL for you, executing it, and packaging up the result so that it looks to the calling code as though it actually was an in-memory collection to begin with.
Next piece of magic is expressions. The parameter to the Where method above,
i => i > 5is a lambda expression, or an anonymous method, in most cases, and you could actually declare it like this for an in-memory collection:However, expression support in C# means that you can also declare it as:
Here, the compiler isn’t actually storing it as a compiled piece of code, but rather an in-memory data structure that knows that it takes one argument, compares it to 5 with a greater-than comparison and returns the result. Note that you have to use the lambda way of writing it, not as a delegate.
This knowledge allows those other Where implementations, if they’re declared to take expressions, to not only get a hold of the “where clause”, but to look at it, pick it apart, and rewrite it.
Which means that generating that SQL can be done in the Where method that knows how to deal with SQL code.
Here’s the LINQ to SQL declaration of the Where method: Queryably.Where.
So LINQ is the combination of many smaller pieces of technology added to the C# compiler: