NHibernate - QueryOver 条件出现在 Where 而不是 Having 子句中,错误

NHibernate - QueryOver criteria appearing in Where instead in Having clause, error

我在使用 Group by 的 QueryOver 中遇到问题,并且在 where 子句中有一些条件。想要在 Having 子句中移动一些具有 SUM() 值的条件,但每次它出现在 Where 子句中并导致错误。 **Error** ="*An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference*"

Conjunction conjunction = Restrictions.Conjunction();
Conjunction havingconjun = Restrictions.Conjunction();

conjunction.Add<Vendor>(p => v.Name == "Some Vendor");
havingconjun.Add(Restrictions.Gt(
   Projections.Sum(Projections.Property(() => v.Payments),
   Convert.ToDouble(SomeInvoice.Value)));

var reportModels =
            Session.QueryOver<Vendor>(() => v)
    .Where(conjunction)
    .Where(havingconjun)
    .SelectList(list => list
                    .SelectGroup(() => v.Number).WithAlias(() => vModel.VendorNumber)
                    .SelectGroup(() => vtypeCode.Code).WithAlias(() => vModel.VendorType)
                    .SelectGroup(() => v.Name).WithAlias(() => vModel.VendorName))
             .TransformUsing(Transformers.AliasToBean<VendorAnalysisReportModel>())
             .List<VendorAnalysisReportModel>();

预期结果:

SELECT 
    V.VENDORNUMBER, V.VENDORTYPE, V.VENDORNAME, SUM(V.PAYMENTS)
 FROM VENDOR V
    WHERE V.NAME = "Some Vendor"
 GROUP 
    BY V.VENDORNUMBER, V.VENDORTYPE, V.VENDORNAME
 HAVING SUM(V.PAYMENTS) > somevalue

现在开始:

SELECT 
    V.VENDORNUMBER, V.VENDORTYPE, V.VENDORNAME, SUM(V.PAYMENTS)
 FROM VENDOR V
    WHERE V.NAME = "Some Vendor" AND
    SUM(V.PAYMENTS) > somevalue
 GROUP 
    BY V.VENDORNUMBER, V.VENDORTYPE, V.VENDORNAME

因为很多人在 NHibernate 中找不到这个问题的解决方案,所以我使用了一些简单的技巧来实现我的结果,我可以说这个问题的解决方案,直到 NHibernate 修复它。

从 having 和 运行 简单查询中获取删除条件后,它看起来像这样。

var reportModels =
            Session.QueryOver<Vendor>(() => v)
    .Where(conjunction)
    .SelectList(list => list
                    .SelectGroup(() => v.Number).WithAlias(() => vModel.VendorNumber)
                    .SelectGroup(() => vtypeCode.Code).WithAlias(() => vModel.VendorType)
                    .SelectGroup(() => v.Name).WithAlias(() => vModel.VendorName))
             .TransformUsing(Transformers.AliasToBean<VendorAnalysisReportModel>())
             .List<VendorAnalysisReportModel>();

var vlst2 =
                    (from vendrs in reportModels orderby vendrs.VendorName ascending select vendrs)
                        .ToList<VendorAnalysisReportModel>().AsQueryable();

然后您可以在任何字段上放置任意数量的 where 子句。

vlst2 = vlst2.Where(p => p.OutstandingComm > Convert.ToDecimal(toDateComAmount.Value));

vlst2 = vlst2.Where(p => p.ToDateOrders < Convert.ToDecimal(toDateOrdAmount.Value));

我的问题已解决,复杂报告 运行 成功,我们在其他查询中也遵循同样的做法。

QF