对聚合函数求和的CreateCriteria排序

时间:2013-06-13 11:14:08

标签: nhibernate fluent-nhibernate createcriteria

SELECT 
  [ProductErpIdentifier], 
  [PriceValue], 
  [ManufacturerProductIdentifier], 
  SUM([QuantityOrdered]) as  [QuantityOrdered]
FROM 
  [eCommerceDev].[dbo].[OrderItem] 
GROUP BY 
  [ProductErpIdentifier],[PriceValue],[ManufacturerProductIdentifier]
ORDER BY  
  [QuantityOrdered] desc

如何编写CreateCriteria以生成SQL,如上例所示?

1 个答案:

答案 0 :(得分:0)

这应该可以解决问题。由于您没有发布订单的映射,我使用了字段名作为属性名称。您需要将OrderItemEntity更改为您自己的映射类,并且需要更改以oi开头的名称。通过类中属性的名称(请注意:这些字符串区分大小写!)

var yourResult = Session.CreateCriteria<OrderItemEntity>("oi")
                   .SetProjection(Projections.ProjectionList()
                      .Add(Projections.Alias(Projections.GroupProperty("oi.ProductErpIdentifier"), "ProductErpIdentifier"))
                      .Add(Projections.Alias(Projections.GroupProperty("oi.PriceValue"), "PriceValue"))
                      .Add(Projections.Alias(Projections.GroupProperty("oi.ManufacturerProductIdentifier"), "ManufacturerProductIdentifier"))
                      .Add(Projections.Alias(Projections.Sum("oi.QuantityOrdered"), "QuantityOrdered")))
                   .AddOrder(Order.Desc("QuantityOrdered"))
                   .List();