Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>LINQ is many things, it's the combination of many smaller things.</p> <p>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.</p> <p><strong>LINQ</strong> stands for "<b>L</b>anguage <b>IN</b>tegrated <b>Q</b>uery", and the most naive interpretation is that they added SQL-like syntax to the C# programming language.</p> <p>So instead of:</p> <pre><code>IEnumerable&lt;int&gt; values = otherValues.Where(i =&gt; i &gt; 5); </code></pre> <p>they have the syntax:</p> <pre><code>IEnumerable&lt;int&gt; values = from i in otherValues where i &gt; 5 select i; </code></pre> <p>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.</p> <p>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".</p> <p>So the first piece of code above:</p> <pre><code>IEnumerable&lt;int&gt; values = otherValues.Where(i =&gt; i &gt; 5); </code></pre> <p>actually ends up being compiled as:</p> <pre><code>IEnumerable&lt;int&gt; values = Enumerable.Where(otherValues, i =&gt; i &gt; 5); </code></pre> <p>The Where method is defined <a href="http://msdn.microsoft.com/en-us/library/bb534803.aspx" rel="nofollow noreferrer">here: Enumerable.Where</a>.</p> <p>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 <code>List&lt;T&gt;</code> where <code>T</code> is an <code>int</code>, and then find that <code>Enumerable.Where</code> is the one to call.</p> <p>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.</p> <p>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.</p> <p>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.</p> <p>Next piece of magic is expressions. The parameter to the Where method above, <code>i =&gt; i &gt; 5</code> is a lambda expression, or an anonymous method, in most cases, and you could actually declare it like this for an in-memory collection:</p> <pre><code>Func&lt;int, bool&gt; w = delegate(int i) { return i &gt; 5; }; IEnumerable&lt;int&gt; values = otherValues.Where(w); </code></pre> <p>However, expression support in C# means that you can also declare it as:</p> <pre><code>Expression&lt;Func&lt;int, bool&gt;&gt; w = i =&gt; i &gt; 5; </code></pre> <p>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.</p> <p>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.</p> <p>Which means that generating that SQL can be done in the Where method that knows how to deal with SQL code.</p> <p>Here's the LINQ to SQL declaration of the Where method: <a href="http://msdn.microsoft.com/en-us/library/system.linq.queryable.where.aspx" rel="nofollow noreferrer">Queryably.Where</a>.</p> <p>So LINQ is the combination of many smaller pieces of technology added to the C# compiler:</p> <ul> <li><a href="http://msdn.microsoft.com/en-us/library/bb397676.aspx" rel="nofollow noreferrer">LINQ syntax</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx" rel="nofollow noreferrer">Extension methods</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable_members.aspx" rel="nofollow noreferrer">LINQ extension methods</a> (+ other implementations, in particular look at <a href="http://msdn.microsoft.com/en-us/library/bb425822.aspx" rel="nofollow noreferrer">LINQ to SQL</a>.)</li> <li><a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx" rel="nofollow noreferrer">Lambda expressions</a> and <a href="http://msdn.microsoft.com/en-us/library/bb397951.aspx" rel="nofollow noreferrer">Expression trees</a>.</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload