Multiple linq "from" and variables visibility

Viewed 359

I am trying to understand why this linq does not compile (fundInvoices aren't visible):

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)
}

So I went on to change this to anonymous type usage, and fundInvoices are magically visible here:

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)
               }
};

But that anonymous type seems to be redundant, I do not make any use of that. I just need a flat list of Payment objects. However my code only compiles that way...

2 Answers
Related