我試圖理解爲什麼這個LINQ不會編譯(fundInvoices不可見):多LINQ「從」和變量的知名度
Dictionary<Fund, IEnumerable<Invoice>> paidfundInvoices;
...
from fundInvoices in paidfundInvoices
from p in fundInvoices.Value
group p by p.VendorId into ps
select new Payment
{
FundId = fundInvoices.Key.FundId, // ERROR here
Value = ps.Sum(p => p.Amount)
}
於是我去到這個改變匿名類型的使用和fundInvoices是神奇可見這裏:
from fundInvoices in paidfundInvoices
select new
{
Fund = fundInvoices.Key,
Payments = from p in fundInvoices.Value
group p by p.VendorId into ps
select new Payment
{
FundId = fundInvoices.Key.FundId, // NO ERROR
Value = ps.Sum(p => p.Amount)
}
};
但是,匿名類型似乎是多餘的,我不作任何利用這一點。我只需要一個支付對象的平面清單。然而,我的代碼只能編譯這種方式...