嗨我正在编写我的方式通过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个集合(供应商和客户)返回一个对象。是不是应该以某种方式加入他们?
我想我不明白群组加入是如何工作的。
每次我尝试这样的查询,我得到的错误,“在‘群组加入’操作必须跟一个‘的SelectMany’操作,其中集合选择器调用‘DefaultIfEmpty’方法。”那是怎么回事? – Boydski 2013-12-02 19:04:29
@Boydski从来没有面对过这个错误。你可以发布你的问题作为重现步骤的问题吗? – 2013-12-02 19:48:32
http://stackoverflow.com/questions/20336653/outer-join-with-linq-causing-groupjoin-error – Boydski 2013-12-02 19:55:10