2011-05-26 63 views
0

首先我知道GroubBy多个属性尚未实现。Nhibernate GroupBy倍数和计数

我想要做的是

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 
+0

会发生什么它? – 2011-05-26 13:26:35

+0

编辑添加异常 – 2011-05-26 13:32:08

+0

Antlr异常似乎是由于不正确的HQL语法。 请参阅:http://stackoverflow.com/questions/3799087/nhibernate-antlr-runtime-noviablealtexception。 像上面的链接一样,QuerySyntaxException是否包含异常? – Tomas 2011-05-26 14:07:34

回答

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似乎不支持外子查询选择和其中:http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-subqueries

否则,你可以创建一个存储过程,并在你的映射添加:当您运行Correct NHibernate Mapping For Stored Procedure?

+0

不会返回整组结果并将它们计入内存中吗? – 2011-05-26 15:34:32