首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Restrictions.Discjunction进行投影

使用Restrictions.Discjunction进行投影
EN

Stack Overflow用户
提问于 2011-07-21 12:20:02
回答 1查看 370关注 0票数 0

使用Restrictions.Discjunction()或Restrictions.Or()进行投影将生成WHERE子句而不是HAVING子句。这将导致错误( WHERE子句不能引用聚合表达式)。

示例:

代码语言:javascript
复制
.CreateCriteria(typeof(SalesOrderLine), "SOL")
.CreateCriteria("DeliveryOrderLines", "DOL", JoinType.LeftOuterJoin)
.SetProjection(
    Projections.GroupProperty("SOL.Key").As("SalesOrderLineId"),
    Projections.GroupProperty("SOL.Item"),
    Projections.GroupProperty("SOL.Description"),
    Projections.GroupProperty("SOL.UnitPrice"),
    Projections.GroupProperty("SOL.Quantity"),
    Projections.GroupProperty("SOL.DiscountPercentage"),
    Projections.GroupProperty("SOL.SalesOrder"))
.Add(Restrictions.Or(
    Restrictions.IsNull(Projections.Sum("DOL.Quantity")),
    Restrictions.GtProperty("SOL.Quantity", Projections.Sum("DOL.Quantity")))), 
.List();

SQL结果:

代码语言:javascript
复制
SELECT this_.SalesOrderLineId as y0_, this_.Item as y1_, this_.Description as y2_, this_.UnitPrice as y3_, this_.Quantity as y4_, this_.DiscountPercentage as y5_, this_.SalesOrderId as y6_ FROM SalesOrderLine this_ left outer join DeliveryOrderLine dol1_ on this_.SalesOrderLineId=dol1_.SalesOrderLineId 
WHERE (sum(dol1_.Quantity) is null or this_.Quantity > sum(dol1_.Quantity))
GROUP BY this_.SalesOrderLineId, this_.Item, this_.Description, this_.UnitPrice, this_.Quantity, this_.DiscountPercentage, this_.SalesOrderId

我做错了吗?或者这是Nhibernate 3.1中的一个bug?

提前感谢:)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-21 13:04:00

上次我检查条件API不支持HAVING子句。您将需要使用HQL,它确实支持具有。

要使用Criteria API,您必须将查询修改为:

代码语言:javascript
复制
SELECT this_.SalesOrderLineId as y0_, this_.Item as y1_, this_.Description as y2_, this_.UnitPrice as y3_, this_.Quantity as y4_, this_.DiscountPercentage as y5_, this_.SalesOrderId as y6_ FROM SalesOrderLine this_ left outer join DeliveryOrderLine dol1_ on this_.SalesOrderLineId=dol1_.SalesOrderLineId 
WHERE (select sum(Quantity) from DeliveryOrderLine) is null or (select sum(Quantity) from > SalesOrderLine ) > (select sum(Quantity) from DeliveryOrderLine)
GROUP BY this_.SalesOrderLineId, this_.Item, this_.Description, this_.UnitPrice, this_.Quantity, this_.DiscountPercentage, this_.SalesOrderId

我给出的解决方案会因为多次求和计算而对性能造成一些影响,但它会完成工作。

您也可以尝试这样做,但我不确定它是否能正确运行:

代码语言:javascript
复制
SELECT this_.SalesOrderLineId as y0_, this_.Item as y1_, this_.Description as y2_, this_.UnitPrice as y3_, this_.Quantity as y4_, this_.DiscountPercentage as y5_, this_.SalesOrderId as y6_, SUM(dol1_Quantity) as sum1, SUM(this_.Quantity) as sum2 from SalesOrderLine this_ left outer join DeliveryOrderLine dol1_ on this_.SalesOrderLineId=dol1_.SalesOrderLineId 
WHERE sum1 is null or sum1 > sum2
GROUP BY this_.SalesOrderLineId, this_.Item, this_.Description, this_.UnitPrice, this_.Quantity, this_.DiscountPercentage, this_.SalesOrderId

希望这能有所帮助!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6771210

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档