0

I found the following linq expression, and I wonder how does the into keyword translate to in method based syntax?

var q = from pd in dataContext.tblProducts 
        join od in dataContext.tblOrders on pd.ProductID equals od.ProductID 
        into t 
        from rt in t.DefaultIfEmpty() 
        orderby pd.ProductID 
        select pd
Marco
  • 22,856
  • 9
  • 75
  • 124
kasperhj
  • 10,052
  • 21
  • 63
  • 106

2 Answers2

2

All these transformations are described in C# specification. 7.16.2 Query expression translation is all about that part of C#.

According to that specification there are two cases for join with an into:

A query expression with a join clause with an into followed by a select clause

from x1 in e1
join x2 in e2 on k1 equals k2 into g
select v

is translated into

( e1 ) . GroupJoin( e2 , x1 => k1 , x2 => k2 , ( x1 , g ) => v )

A query expression with a join clause with an into followed by something other than a select clause

from x1 in e1
join x2 in e2 on k1 equals k2 into g
…

is translated into

from * in ( e1 ) . GroupJoin(
  e2 , x1 => k1 , x2 => k2 , ( x1 , g ) => new { x1 , g })
…
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
1

According to this post by @GertArnold, group ... into linq query syntax uses GroupJoin() method under the hood :

var q1 = from pd in dataContext.tblProducts 
        join od in dataContext.tblOrders on pd.ProductID equals od.ProductID 
        into t
        select t;

var q2 = dataContext.tblProducts
                    .GroupJoin(dataContext.tblOrders
                                , pd => pd.ProductID
                                , od => od.ProductID
                                , (pd, ods) => select new 
                                                    {
                                                        Product = pd,
                                                        Orders = ods
                                                    });

Both expression in sample above do grouping operation in similar way, despite the return value are different.

Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137