title: Linq query to sum values from tables
body: <p>I have a LINQ query, which takes an invoice, looks up the quantity and unit price of each item attached, and in another linked table, looks up any payments made against the invoice.</p>
  <p>Currently this is:</p>
  <pre><code>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
      }
  }
  </code></pre>
  <p>This shows the following results:</p>
  <p><img src="https://i.stack.imgur.com/1Mixl.png" alt="screenshot of results" /></p>
  <p>How do I change this LINQ query to show just the sum of SubPrice and SumPayments (ie:</p>
  <blockquote>
  <p>Invoice 11: SubPrice= 90.00  SumPayments= 24.00</p>
  <p>Invoice 12: SubPrice=175.00, SumPayments=175.00</p>
  </blockquote>
  <p>Thank you for any help,</p>
  <p>Mark</p>
  <p><strong>UPDATE SHOWING WORKING ANSWER FOLLOWING ANSWER FROM KENNETH</strong></p>
  <pre><code>from i in Invoices
  select new
  {
      InvoiceID = i.InvoiceId, 
      CustomerName = i.CustomerName,
      SubPrice = InvoiceItems.Where(it =&gt; it.InvoiceId == i.InvoiceId).Select(it =&gt; it.Quantity*it.UnitPrice).Sum(),
      SumPayments = PaymentInvoices.Where(pi =&gt; pi.InvoiceId == i.InvoiceId).Select(pi =&gt; pi.AmountAllocated).Sum()
  }
  </code></pre>