nHibernate-在中添加聚合函数。选择

nHibernate-在中添加聚合函数。选择,nhibernate,aggregate-functions,queryover,Nhibernate,Aggregate Functions,Queryover,我试图在QueryOver中实现一个业务公式 POorder.Estimate是一个计算字段,我需要 ToDateOrderAmount = POrder.Estimate - Sum(PODist.Field1) - Sum(PODisTaxRebate.Field1 + PODisTaxRebate.Field2) 所以我需要写一个查询。我现在得到的是: var reportModels = Session.QueryOver<Domain.Model.Pur

我试图在QueryOver中实现一个业务公式

POorder.Estimate
是一个计算字段,我需要

ToDateOrderAmount = POrder.Estimate - Sum(PODist.Field1) - Sum(PODisTaxRebate.Field1 + PODisTaxRebate.Field2)
所以我需要写一个查询。我现在得到的是:

    var reportModels =
        Session.QueryOver<Domain.Model.Purchasing.Vendor>(() => v)
            .Left.JoinQueryOver(() => v.Invoices, () => invoice)
            .Left.JoinQueryOver(() => invoice.PurchaseOrder, () => poOrder)
            .Left.JoinQueryOver(() => poOrder.PurchaseOrderDistributions, () => poDistribution)
            .Left.JoinQueryOver(() => poDistribution.TaxRebate, () => poTaxRebate)
            .SelectList(
                list =>
                list.Select(() => v.Number).WithAlias(() => varptModel.VendorNumber)
                    .Select(() => v.TypeCode.Code).WithAlias(() => varptModel.VendorType)
                    .Select(() => v.Name).WithAlias(() => varptModel.VendorName)
                    .Select(() => v.PurchasingContactPhoneNumber + "-Ext." + v.PurchasingContactPhoneNumberExt).WithAlias(() => varptModel.Phone)
                    .Select(() => v.Address).WithAlias(() => varptModel.Address)
                    .Select(() => invFiscalYear.Year).WithAlias(() => varptModel.Year)
                    .Select(() => invoice.TotalAmount).WithAlias(() => varptModel.InvoiceToDate)
                    .Select(() => invoice.AmountPaidToDate).WithAlias(() => varptModel.PaymentToDate)
                    .Select(() => poOrder.Estimate).WithAlias(() => varptModel.OrdersToDate)
        .Select(() => poOrder.Estimate - Sum(poDistribution.Field1) - Sum(poTaxRebate.Discount1 + poTaxRebate.Discount2) )
                    ).List();
var报告模型=
Session.QueryOver(()=>v)
.Left.JoinQueryOver(()=>v.发票,()=>invoice)
.Left.JoinQueryOver(()=>invoice.PurchaseOrder,()=>poOrder)
.Left.JoinQueryOver(()=>poOrder.PurchaseOrderDistributions,()=>poDistribution)
.Left.JoinQueryOver(()=>poDistribution.taxreip,()=>potaxreip)
.选择列表(
列表=>
列表。选择(()=>v.Number)。使用别名(()=>varptModel.VendorNumber)
.选择(()=>v.TypeCode.Code)。使用别名(()=>varptModel.VendorType)
.Select(()=>v.Name).WithAlias(()=>varptModel.VendorName)
.选择(()=>v.PurchasingContactPhoneNumber+“-Ext.”+v.PurchasingContactPhoneNumber).with别名(()=>varptModel.Phone)
.选择(()=>v.Address)。使用别名(()=>varptModel.Address)
.选择(()=>invFiscalYear.Year)。使用别名(()=>varptModel.Year)
。选择(()=>invoice.TotalAmount)。使用别名(()=>varptModel.InvoiceToDate)
。选择(()=>invoice.AmountPaidToDate)。使用别名(()=>varptModel.PaymentToDate)
.Select(()=>poOrder.Estimate)。使用别名(()=>varptModel.OrdersToDate)
.选择(()=>poOrder.Estimate-Sum(poDistribution.Field1)-Sum(potaxreip.Discount1+potaxreip.Discount2))
).List();

但这是不对的。我应该把它改成什么?

我尝试了很多方法,发现这很有效

.Select(Projections.SqlFunction(new VarArgsSQLFunction("", "+", ""),
                                NHibernateUtil.Double,
                                Projections.SqlFunction(new VarArgsSQLFunction("", "+", ""),
                                NHibernateUtil.Double,
                                Projections.Sum(Projections.SqlFunction("coalesce", NHibernateUtil.Double, Projections.Property(() => invoiceLineItem.Expense), Projections.Constant(0))),
                                Projections.Sum(Projections.SqlFunction("coalesce", NHibernateUtil.Double, Projections.Property(() => invitemTaxRebate.Rebate1Expense), Projections.Constant(0)))),
                                Projections.Sum(Projections.SqlFunction("coalesce", NHibernateUtil.Double, Projections.Property(() => invitemTaxRebate.Rebate2Expense), Projections.Constant(0)))))
                                .WithAlias(() => varptModel.ToDateInvoices)
这给了我这个SQL

sum(coalesce(invoicelin8_.Expense, 0 )) + sum(coalesce(invitemtax9_.Rebate1Expense, 0 )) + sum(coalesce(invitemtax9_.Rebate2Expense, 0 )) 

我添加了Coalesce,因为当我们用null值加上或减去值时,所有值在结果中都变成null。只是一个新的提示

你想生成什么样的SQL?首先很高兴看到你,因为我一个月前开始从你的博客或网站学习nHibernate。在这里,我试图在QueryOver中实现一个业务公式。POorder.Estimate是一个计算字段,其中需要获取ToDateOrderAmount=POrder.Estimate-Sum(PODist.Field1)-Sum(PODISTAXREIT.Field1+PODISTAXREIT.Field2)。如果您愿意,我可以发送整个查询。将信息从注释移动到正文中。然而,这篇文章仍然需要更清楚地解释问题是什么,使用提供的代码时会发生什么,等等。感谢Kategergory改进了我的问题。你现在遇到了什么错误?此外,必须按属性分组才能使用
Sum