2013-02-25 91 views
2

嗨我正在編寫我的方式通過MS 101 linq示例。Linq SelectMany

的「JoinOperators」是給我一個困難時期,因爲我試圖重構查詢表達式lambda語法,反之亦然。

無論如何,在例如105我看到這個查詢表達式:

var supplierCusts = 
    from sup in suppliers 
    join cust in customers on sup.Country equals cust.Country into cs 
    from c in cs.DefaultIfEmpty() // DefaultIfEmpty preserves left-hand elements that have no matches on the right side 
    orderby sup.SupplierName 
    select new 
    { 
     Country = sup.Country, 
     CompanyName = c == null ? "(No customers)" : c.CompanyName, 
     SupplierName = sup.SupplierName 
    }; 

,我試圖將其實現爲拉姆達這樣:

// something is not right here because the result keeps a lot of "Join By" stuff in the output below 
var supplierCusts = 
    suppliers.GroupJoin(customers, s => s.Country, c => c.Country, (s, c) => new { Customers = customers, Suppliers = suppliers }) 
     .OrderBy(i => i.Suppliers) // can't reference the "name" field here? 
     .SelectMany(x => x.Customers.DefaultIfEmpty(), (x, p) => // does the DefaultIfEmpty go here? 
      new 
      { 
       Country = p.Country, 
       CompanyName = x == null ? "(No customers)" : p.CompanyName, 
       SupplierName = p // not right: JoinOperators.Program+Customer ... how do I get to supplier level? 
      }); 

出於某種原因,我無法訪問供應商這種方式的信息。當我與供應商交換客戶時,我無法訪問客戶級信息。

是否存在的的SelectMany一些超載,讓我從兩個對象的字段級拉?

另外,我不明白爲什麼會出現羣組加入用2個集合(供應商和客戶)返回一個對象。是不是應該以某種方式加入他們?

我想我不明白羣組加入是如何工作的。

回答

5

你有錯誤的結果選擇組加入,這就是問題的開始。這裏是固定的查詢:

var supplierCusts = 
    suppliers 
    .GroupJoin(customers, 
       sup => sup.Country, 
       cust => cust.Country, 
       (sup, cs) => new { sup, cs }) 
    .OrderBy(x => x.sup.Name)  
    .SelectMany(x => x.cs.DefaultIfEmpty(), (x, c) => 
     new 
     { 
      Country = x.sup.Country, 
      CompanyName = c == null ? "(No customers)" : c.CompanyName, 
      SupplierName = x.sup.Name 
     }); 
+0

每次我嘗試這樣的查詢,我得到的錯誤,「在‘羣組加入’操作必須跟一個‘的SelectMany’操作,其中集合選擇器調用‘DefaultIfEmpty’方法。」那是怎麼回事? – Boydski 2013-12-02 19:04:29

+0

@Boydski從來沒有面對過這個錯誤。你可以發佈你的問題作爲重現步驟的問題嗎? – 2013-12-02 19:48:32

+0

http://stackoverflow.com/questions/20336653/outer-join-with-linq-causing-groupjoin-error – Boydski 2013-12-02 19:55:10

1

如果您想了解翻譯查詢表達式爲拉姆達的,我建議你檢查出LinqPad默認可以做到這一點。

Suppliers 
    .GroupJoin (
     Customers, 
     sup => sup.Country, 
     cust => cust.Country, 
     (sup, cs) => 
     new 
     { 
      sup = sup, 
      cs = cs 
     } 
    ) 
    .SelectMany (
     temp0 => temp0.cs.DefaultIfEmpty(), 
     (temp0, c) => 
     new 
     { 
      temp0 = temp0, 
      c = c 
     } 
    ) 
    .OrderBy (temp1 => temp1.temp0.sup.CompanyName) 
    .Select (
     temp1 => 
     new 
     { 
      Country = temp1.temp0.sup.Country, 
      CompanyName = (temp1.c == null) ? "(No customers)" : temp1.c.CompanyName, 
      SupplierName = temp1.temp0.sup.CompanyName 
     } 
    ) 

話雖這麼說,我通常會發現到的SelectMany更容易編寫和維護使用的查詢語法而不是lambda語法:例如,如下查詢翻譯。

的羣組加入在此實例中用於完成左連接(經由.DefaultIfEmpty子句)。

1

試試這個:

var supplierCusts = 
    suppliers.GroupJoin(customers, s => s.Country, c => c.Country, (s, c) => new { Supplier = s, Customers = c }) 
     .OrderBy(i => i.Supplier.SupplierName) 
     .SelectMany(r => r.Customers.DefaultIfEmpty(), (r, c) => new 
     { 
      Country = r.Supplier.Country, 
      CompanyName = c == null ? "(No customers)" : c.CompanyName, 
      SupplierName = r.Supplier.SupplierName 
     });