嗨我正在編寫我的方式通過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