2014-02-27 31 views
0

found以下linq表達式的基於方法的語法等效,我想知道如何將into關鍵字轉換爲基於方法的語法?什麼是加入到

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 

回答

1

根據this post@GertArnoldgroup ... into LINQ查詢語法使用引擎蓋下GroupJoin()方法:以上

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 
                }); 

兩種表達在樣品做分組在相似的方式操作,儘管返回值是不同的。

2

所有這些轉換都在C#規範中描述。 7.16.2查詢表達式轉換是關於C#的那部分內容。

根據該規範有兩種情況爲joininto

的查詢表達式與join子句與into隨後是 select子句

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

被翻譯成

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

的查詢表達式與join子句與into,隨後通過除select子句其他 東西

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

被翻譯成

from * in (e1) . GroupJoin(
    e2 , x1 => k1 , x2 => k2 , (x1 , g) => new { x1 , g }) 
…