Possible Duplicate:
Learning about LINQ
Hi everyone,
I just want to understand what exactly is LINQ in DOTNET? And How does it work?
Tx
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:
IEnumerable<int> values = otherValues.Where(i => i > 5);
they have the syntax:
IEnumerable<int> values = from i in otherValues
where i > 5
select i;
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:
IEnumerable<int> values = otherValues.Where(i => i > 5);
actually ends up being compiled as:
IEnumerable<int> values = Enumerable.Where(otherValues, i => i > 5);
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>
where T
is an int
, and then find that Enumerable.Where
is 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 > 5
is a lambda expression, or an anonymous method, in most cases, and you could actually declare it like this for an in-memory collection:
Func<int, bool> w = delegate(int i) { return i > 5; };
IEnumerable<int> values = otherValues.Where(w);
However, expression support in C# means that you can also declare it as:
Expression<Func<int, bool>> w = i => i > 5;
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:
MSDN does a very good job of introducing LINQ:
[...]
.NET Language-Integrated Query defines a set of general purpose standard query operators that allow traversal, filter, and projection operations to be expressed in a direct yet declarative way in any .NET-based programming language. The standard query operators allow queries to be applied to any IEnumerable-based information source. LINQ allows third parties to augment the set of standard query operators with new domain-specific operators that are appropriate for the target domain or technology. More importantly, third parties are also free to replace the standard query operators with their own implementations that provide additional services such as remote evaluation, query translation, optimization, and so on. By adhering to the conventions of the LINQ pattern, such implementations enjoy the same language integration and tool support as the standard query operators.
[...]
It is several different things.
Linq, as it appears in the System.Linq namespace is a set of extension methods that allow you to query collections directly in code. It is an acronym for "Language INtegrated Query".
It is also a set of providers that allow you to query different data sources - SQL with Linq to SQL, XML with Linq to XML and more.
Think it as querying objects using a sql-like syntax. Here is an example copied from http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx
public void Linq1()
{
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
var lowNums =
from n in numbers
where n < 5
select n;
Console.WriteLine("Numbers < 5:");
foreach (var x in lowNums)
{
Console.WriteLine(x);
}
}
Linq is a set of extension methods on to of IEnumerable. It's meant to let you abstract away some of the details of getting objects out of collections. When you 'query' your collection via Linq, you do it in a way that's declarative instead of imperative. What that means is that your Linq query shows what you want to get, instead of precisely how you're going to get it. In a foreach() loop, you're going to have to be really explicit about how you're going to filter, group, and sort your results. With Linq, it's just a few short statements and the implementation details are abstracted away from you.
A lot of people have the misconception that it has to do with SQL because of Linq-to-Sql, but really that's just one small part of Linq. You don't have to use L2S to get full the power of Linq, and in fact many people don't. Though Linq-to-SQL in my personal opinion is the cat's meow if you're a .NET shop with just SQL Server as your DB.
LINQ
on Google? - Winston Smith