3

我有一个 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
    }
}

这显示了以下结果:

结果截图

如何更改此 LINQ 查询以仅显示 SubPrice 和 SumPayments 的总和(即:

发票 11:SubPrice= 90.00 SumPayments= 24.00

发票 12:SubPrice=175.00,SumPayments=175.00

感谢您的任何帮助,

标记

更新显示肯尼斯回答后的工作答案

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

1 回答 1

4

您可以使用以下方法:

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()
}
于 2013-05-17T12:50:40.907 回答