嗯,基本上我试图找出一种方法来获得10个最畅销的产品。东西我可以很容易地用下面的SQL查询实现:C#/ NHibernate - 获取前10条记录按分组总数排序
SELECT `product`.*
FROM `product`
INNER JOIN `sale_item` ON `product`.`id` = `sale_item`.`product_id`
GROUP BY `product`.`id`
ORDER BY SUM(`sale_item`.`quantity`) DESC
LIMIT 10;
我与NHibernate的成功最接近的,我相信是这样的:
ICriteria criteria = NHibernateSession
.CreateCriteria<SaleItem>("SaleItem")
.SetMaxResults(10)
.CreateCriteria("ID.Product")
.SetProjection(Projections.ProjectionList()
.Add(Projections.GroupProperty("ID.Product"))
.Add(Projections.Sum("SaleItem.Quantity"), "QuantitySum")
)
.AddOrder(Order.Desc("QuantitySum"));
List<Product> l = criteria
.List<Product>() as List<Product>;
即生成以下SQL(非常类似地雷) :
SELECT this_.product_id AS y0_,
sum(this_.quantity) AS y1_
FROM sale_item this_
INNER JOIN product product1_ ON this_.product_id=product1_.id
GROUP BY this_.product_id
ORDER BY y1_ DESC LIMIT 10;
不幸的是,它在执行查询时失败。我很确定这跟我做.CreateCriteria<SaleItem>
有什么关系,然后问.List<Product>
,但我不知道如何以其他方式做到这一点。
任何帮助深表谢意。