2013-05-17 69 views
1

我有一个LINQ查询,它需要一张发票,查找每个附加项目的数量和单价,并在另一个链接表中查找对发票进行的任何付款。Linq查询来总结表中的值

目前是这样的:

from i in Invoices 
select new 
{ 
i.InvoiceId, i.CustomerName, 
Items = 
    from it in InvoiceItems 
    where i.InvoiceId == it.InvoiceId 
    select new { 
    it.Item, 
    it.Quantity, 
    it.UnitPrice, 
    SubPrice=it.Quantity*it.UnitPrice 
    }, 
Payments = 
    from pi in PaymentInvoices 
    where i.InvoiceId == pi.InvoiceId 
    select new { 
    SumPayments=pi.AmountAllocated 
    } 
} 

这显示了以下结果:

screenshot of results

如何改变这个LINQ查询,以显示SubPrice和SumPayments(即只总和:

发票11:SubPrice = 90.00 SumPayments = 24.00

发票12:SubPrice = 175.00,SumPayments = 175.00

感谢您的帮助,

马克

UPDATE指示工作ANSWER以下的答案给Kenneth

from i in Invoices 
select new 
{ 
    InvoiceID = i.InvoiceId, 
    CustomerName = i.CustomerName, 
    SubPrice = InvoiceItems.Where(it => it.InvoiceId == i.InvoiceId).Select(it => it.Quantity*it.UnitPrice).Sum(), 
    SumPayments = PaymentInvoices.Where(pi => pi.InvoiceId == i.InvoiceId).Select(pi => pi.AmountAllocated).Sum() 
} 

回答

3

您可以使用以下方法:

from i in Invoices 
select new 
{ 
    InvoiceID = i.InvoiceId, 
    CustomerName = i.CustomerName, 
    SubPrice = InvoiceItems.Where(it => it.InvoiceID = i.InvoiceID).Select(it => it.Quantity*it.UnitPrice).Sum(), 
    SumPayments = PaymentInvoice.Where(pi => pi.InvoiceId = i.InvoiceId).Select(pi => pi.AmountAllocated).Sum() 
} 
+0

谢谢@Kenneth - 我知道如果我以更简洁的方式转贴,可能会有所帮助..干杯,马克 – Mark