Nhibernate GroupBy 多个和计数

发布于 2024-11-09 13:55:21 字数 1471 浏览 0 评论 0原文

首先我知道 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()

带来整个集合,然后对其进行计数。

我找到了这个并创建了

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

First off I know GroubBy multiple properties has not been implemented yet.

What I want to do is

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

the best I have in Nhibernate is

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

which brings the whole set and then counts it.

I have found this and created 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>();

which doesn't work. Throws a

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

more exception

[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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅唱々樱花落 2024-11-16 13:55:22

您是否尝试过这样的事情,它将在内存中进行计数?

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 和 where 之外的子查询: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-subqueries

否则,您可以创建一个存储过程,并将其添加到您的映射:正确的 NHibernate 映射存储过程?

Did you try something like this, that will do the counting in memory ?

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 doesn't seem to support subqueries outside select and where : http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-subqueries

Otherwise, you could create a stored procedure, and add it in your mapping : Correct NHibernate Mapping For Stored Procedure?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文