Nhibernate GroupBy multiple和Count

时间:2011-05-26 13:12:27

标签: nhibernate count group-by hql linq-to-nhibernate

首先我知道GroubBy尚未实现多个属性。

我想做的是

SELECT count(*)
FROM ( 
    SELECT this_.SubmissionDate as y0_
    FROM   [Coupon] this_
    WHERE  this_.PaymentOrder_id is null 
    GROUP BY this_.SubmissionDate,
           this_.Deal_id
) AS query

我在Nhibernate中最好的是

Session.QueryOver<Coupon>().Select(Projections.Group<Coupon>(e => e.SubmissionDate),Projections.Group<Coupon>(e => e.Deal.Id)).Future<object[]>().Count()

带来整套,然后计算它。

我找到this并创建了此

var count = Session.CreateQuery("select count(*) from (select c.SubmissionDate from Coupon c where c.PaymentOrder.Id is null group by c.SubmissionDate, c.Deal.Id) as query").FutureValue<Int32>();

哪个不起作用。抛出一个

Exception of type 'Antlr.Runtime.NoViableAltException' was thrown. near line 1, column 21

Exception Details: NHibernate.Hql.Ast.ANTLR.QuerySyntaxException: Exception of type 'Antlr.Runtime.NoViableAltException' was thrown. near line 1, column 21

更多例外

[QuerySyntaxException: Exception of type 'Antlr.Runtime.NoViableAltException' was thrown. near line 1, column 21]
   NHibernate.Hql.Ast.ANTLR.ErrorCounter.ThrowQueryException() +118
   NHibernate.Hql.Ast.ANTLR.HqlParseEngine.Parse() +416

1 个答案:

答案 0 :(得分:0)

你有没有尝试这样的事情,那会在记忆中进行计数吗?

var count = Session.CreateQuery("select c.SubmissionDate from Coupon c where c.PaymentOrder.Id is null group by c.SubmissionDate, c.Deal.Id").ToList().Count;

HQL似乎不支持select以外的子查询,其中:http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-subqueries

否则,您可以创建存储过程,并将其添加到映射中:Correct NHibernate Mapping For Stored Procedure?