こんにちは私はMS101linqの例をコーディングしています。
「JoinOperators」は、クエリ式をラムダ構文に、またはその逆にリファクタリングしようとしているため、苦労しています。
とにかく、例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?
});
何らかの理由で、この方法でサプライヤーレベルの情報にアクセスできません。を切り替えるcustomers
とsuppliers
、顧客レベルの情報にアクセスできません。
両方のオブジェクトのフィールドレベルからプルできる過負荷はありSelectMany()
ますか?
また、が2つのコレクション(および)GroupJoin()
を持つオブジェクトを返すように見える理由もわかりません。どういうわけか彼らに加わることになっていないのですか?suppliers
customers
私はどのように機能するのか理解していないと思いますGroupJoin()
。