title: nHibernate - Adding aggregate functions in .select
body: <p>I am trying to implement a business formula in QueryOver. </p>
  
  <p><code>POorder.Estimate</code> is a calculated field and I need to get </p>
  
  <pre><code>ToDateOrderAmount = POrder.Estimate - Sum(PODist.Field1) - Sum(PODisTaxRebate.Field1 + PODisTaxRebate.Field2)
  </code></pre>
  
  <p>So I need to write a query. What I have now is:</p>
  
  <pre><code>    var reportModels =
          Session.QueryOver&lt;Domain.Model.Purchasing.Vendor&gt;(() =&gt; v)
              .Left.JoinQueryOver(() =&gt; v.Invoices, () =&gt; invoice)
              .Left.JoinQueryOver(() =&gt; invoice.PurchaseOrder, () =&gt; poOrder)
              .Left.JoinQueryOver(() =&gt; poOrder.PurchaseOrderDistributions, () =&gt; poDistribution)
              .Left.JoinQueryOver(() =&gt; poDistribution.TaxRebate, () =&gt; poTaxRebate)
              .SelectList(
                  list =&gt;
                  list.Select(() =&gt; v.Number).WithAlias(() =&gt; varptModel.VendorNumber)
                      .Select(() =&gt; v.TypeCode.Code).WithAlias(() =&gt; varptModel.VendorType)
                      .Select(() =&gt; v.Name).WithAlias(() =&gt; varptModel.VendorName)
                      .Select(() =&gt; v.PurchasingContactPhoneNumber + "-Ext." + v.PurchasingContactPhoneNumberExt).WithAlias(() =&gt; varptModel.Phone)
                      .Select(() =&gt; v.Address).WithAlias(() =&gt; varptModel.Address)
                      .Select(() =&gt; invFiscalYear.Year).WithAlias(() =&gt; varptModel.Year)
                      .Select(() =&gt; invoice.TotalAmount).WithAlias(() =&gt; varptModel.InvoiceToDate)
                      .Select(() =&gt; invoice.AmountPaidToDate).WithAlias(() =&gt; varptModel.PaymentToDate)
                      .Select(() =&gt; poOrder.Estimate).WithAlias(() =&gt; varptModel.OrdersToDate)
          .Select(() =&gt; poOrder.Estimate - Sum(poDistribution.Field1) - Sum(poTaxRebate.Discount1 + poTaxRebate.Discount2) )
                      ).List();
  </code></pre>
  
  <p>But this is not right. What should I change it to?</p>