NHibernate QueryOver with MaxResult, Group By and Order By

倾然丶 夕夏残阳落幕 提交于 2019-12-03 07:11:46

问题


I'm trying to convert a SQL query to NHibernate QueryOver syntax, but I don't understand how to sort by the count projection.

This is what the SQL Query looks like:

select top 10 v.intVoteUserID, COUNT(v.intVoteUserID)
from Group_MessageVotes v
where v.dtmVote > :date
group by v.intVoteUserID
order by COUNT(v.intVoteUserID) desc

Any ideas?


回答1:


You can simply repeat the projection in the OrderBy-clause.

The following query will give you an IList<object[]> where the first element of each item is the id and the second is the count.

var result = session.QueryOver<GroupMessageVotes>()
.Select(
    Projections.Group<GroupMessageVotes>(e => e.intVoteUserID),
    Projections.Count<GroupMessageVotes>(e => e.intVoteUserID)
    )
.OrderBy(Projections.Count<GroupMessageVotes>(e => e.intVoteUserID)).Desc
.Take(10)
.List<object[]>();


来源:https://stackoverflow.com/questions/5708177/nhibernate-queryover-with-maxresult-group-by-and-order-by

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!