nHibernate在子查询中计数行 [英] nHibernate count rows in subquery
问题描述
如何在nHibernate中做这样的事情:
How can I do something like this in nHibernate:
select count(*)
from (subquery)
这是SQL中一个相当简单的查询,但是解决方案在nHibernate中并不那么明显。一个明显的解决方案是沿着以下行:
It is a rather simple query in SQL, but the solution is not so obvious in nHibernate. An obvious solution would be something along the line of:
var rowcount = Session.QueryOver<Entity>()
.Select(Projections.Alias(Projections.Count(Projections.SubQuery(detachedQuery)), "count"))
.FutureValue<int>();
<
However, this results in an ArgumentOutOfRangeException
:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
这 SO答案不适用于我,因为我有一个更复杂的分组。
我的问题源自先前的问题,其中我尝试使用<$
This SO answer doesn't work for me, as I have a more complex grouping.
My question originates from an earlier question where I tried to use ToRowCountQuery
, but that function strips groupings form the query.
推荐答案
我发现一个老帖子由Ayende给我一个解决方案(计算分页数据)。
I found an old post by Ayende which gave me a solution (Counting paged data).
我创建了自己的方言,如该帖中所述,并添加了 rowcount
函数到我的分页查询。和presto,我得到我的行计数在单个查询到数据库。
I created my own dialect as described in that post and added the rowcount
function to my paged query. And presto, I got my rowcount in a single query to the database.
这篇关于nHibernate在子查询中计数行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!