我试图理解为什么这个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)
}
};
但是,匿名类型似乎是多余的,我不作任何利用这一点。我只需要一个支付对象的平面清单。然而,我的代码只能编译这种方式...