Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>For example.. will the first one filter the collection of ListOfComplexClass and then get the StrValue property? Or it'll make some maybe tunning and execute the Select first and then filter the returned collection?</p> </blockquote> <p>(I am assuming that you meant <code>p =&gt; p.StrValue</code> in the <code>Select</code> in the first one.)</p> <p>Actually, it's more subtle than that.</p> <p>Let us say that you take an enumerator on the first one.</p> <pre><code>var lst1 = ListOfComplexClass.Where(p =&gt; p.StrValue == "Whatever") .Select(p =&gt; p.StrValue); var e = lst1.GetEnumerator(); </code></pre> <p>When you call <code>e.MoveNext()</code>, what will happen is that <code>Select</code> will call <code>MoveNext()</code> on an iterator of <code>ListOfComplexClass.Where(p =&gt; p.StrValue == "Whatever")</code> which will call <code>MoveNext()</code> an iterator of <code>ListOfComplexClass</code> until it finds an element <code>p</code> with <code>p.StrValue == "Whatever"</code>. Then, the projected result of this <code>p</code> will be returned as <code>e.Current</code>.</p> <p>Now let us consider the second one. Let us say that you take an enumerator of it.</p> <pre><code>var lst2 = ListOfComplexClass.Select(p =&gt; p.StrValue) .Where(p =&gt; p == "Whatever"); var e = lst2.GetEnumerator(); </code></pre> <p>When you call <code>e.MoveNext()</code>, <code>Where</code> will call <code>MoveNext()</code> on an iterator of <code>ListOfComplexClass.Select(p =&gt; p.StrValue)</code> until it finds an element <code>p</code> with <code>p == "Whatever"</code>. Of course, calling <code>MoveNext()</code> on an iterator of <code>ListOfComplexClass.Select(p =&gt; p.StrValue)</code> will call <code>MoveNext()</code> on an iterator of <code>ListOfComplexClass</code> and return the projection of <code>Current</code> for that iterator.</p> <p>Jon Skeet has a good analogy which I'll steal here. Imagine a deck of cards in random order. Imagine the queries</p> <pre><code>var suits = deck.Where(c =&gt; c.Suit == Suit.Diamond || c.Suit == Suit.Heart) .Select(c =&gt; c.Suit) </code></pre> <p>and</p> <pre><code>var suits = deck.Select(c =&gt; c.Suit) .Where(c =&gt; c == Suit.Diamond || c == Suit.Heart); </code></pre> <p>Now imagine consuming the result of the first query. It goes like this. </p> <p><code>Select</code> asks <code>Where</code> for a card.</p> <p><code>Where</code> asks <code>deck</code> for a card.</p> <p>*<code>Where</code> checks the suit of the card.</p> <p>If the suit of the card is a diamond or a heart, it returns the card to the <code>Select</code> and <code>Select</code> projects the card to its suit and returns it.</p> <p>If the suit of the card is not a diamond or a heart, <code>Where</code> asks for another card from deck and loops back to *.</p> <p>The second query goes like this.</p> <p><code>Where</code> asks <code>Select</code> for a suit.</p> <p><code>Select</code> asks <code>deck</code> for a card.</p> <p><code>deck</code> returns a card to Select.</p> <p><code>Select</code> projects the card to its suit.</p> <p>*<code>Where</code> checks the suit from the <code>Select</code>.</p> <p>If the suit is a diamond or a heart, <code>Where</code> returns the suit.</p> <p>If the suit is not a diamond or a heart, <code>Where</code> asks <code>Select</code> for another suit and loops back to *.</p>
 

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