2013-02-25 56 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 
     });