c# - I need help understanding LINQ. Where is my understanding going wrong? -


i'll give best attempt @ understanding , let me know i'm going wrong.

for simplicity, let's assume live in word has

  • numbers 1, 2, 3, 4, 5
  • operators %, > usual precdence

i want dissamble happens when

list<int> = new list<int> { 1, 2, 3, 4, 5 }; ienumerable<int> filtered = in                             % 2 == 1                             orderby descending                             select i;  foreach ( var in filtererd )  {    console.writeline(i); } 

what understand first of query not create ienumerable<int>; creates , expression tree associated query. elements returned query yielded in invisible function created compiler

public static ienumerable<int> myinvisiblefunction ( list<int> source ) {     foreach ( int in source.reverse() )     {        if ( % 2 == 1 )         {            yield return i;        }      } }  

(of course that's kind of weird example because source.reverse() query, anyways ...)

now i'm confused expression tress come play here. when think of expression trees, think of trees like

        (3 % 1 > 0)           /      \           /        \         (3 % 1)  >    0       /   \       3  %  1 

in small world i've created. tree come in play in linq query

from in % 2 == 1 orderby descending select 

??? that's don't understand. i'm looking @ expression class , see how create example tree showed, don't see come play in query.

i'll give best attempt @ understanding , let me know i'm going wrong.

ok.

what understand first of query not create ienumerable<int>;

this statement wrong.

it creates , expression tree associated query.

this statement wrong.

the elements returned query yielded in invisible function created compiler

this statement wrong.

where tree come in play in linq query

it doesn't. query uses no expression trees.

i'm looking @ expression class , see how create example tree showed, don't see come play

it doesn't.

want dissamble happens when

list<int> = new list<int> { 1, 2, 3, 4, 5 }; ienumerable<int> filtered = in                         % 2 == 1                         orderby descending                         select i;  foreach ( var in filtererd )    console.writeline(i); 

let's break down. first compiler turns into

list<int> = new list<int> { 1, 2, 3, 4, 5 }; ienumerable<int> filtered = all.where(i => % 2 == 1).orderby(i => i);                           foreach ( var in filtererd )    console.writeline(i); 

next compiler overload resolution , evaluates extension methods

list<int> = new list<int> { 1, 2, 3, 4, 5 }; ienumerable<int> filtered =    enumerable.orderby<int>(     enumerable.where<int>(all, => % 2 == 1)),      => i));                            foreach ( var in filtererd )    console.writeline(i); 

next lambdas desugared:

static bool a1(int i) { return % 2 == 1; ) static int a2(int i) { return } ... list<int> = new list<int> { 1, 2, 3, 4, 5 }; ienumerable<int> filtered =    enumerable.orderby<int>(     enumerable.where<int>(all, new func<int, bool>(a1))),      new func<int, int>(a2)));                            foreach (var in filtererd )    console.writeline(i); 

that not how lambdas desugared exactly; cached, let's ignore detail.

i assume not want foreach desugared. see c# specification details.

if want know , orderby do, read source code.


Comments