【问题标题】:NHibernate QueryOver with MaxResult, Group By and Order ByNHibernate QueryOver 与 MaxResult、Group By 和 Order By
【发布时间】:2011-04-18 19:58:56
【问题描述】:

我正在尝试将 SQL 查询转换为 NHibernate QueryOver 语法,但我不明白如何按计数投影进行排序。

这是 SQL 查询的样子:

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

有什么想法吗?

【问题讨论】:

    标签: nhibernate queryover


    【解决方案1】:

    您可以简单地在 OrderBy 子句中重复投影。

    以下查询将为您提供IList<object[]>,其中每个项目的第一个元素是 id,第二个元素是计数。

    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[]>();
    

    【讨论】:

    • 所有这些通用参数都需要吗?
    • @Stefan Steinegger 我认为它们是 lambda 表达式所必需的。可以改写Projections.Count("intVoteUserID"),但我更喜欢第一个选项。
    • 应该可以写Projections.Count(e =&gt; e.intVoteUserID)
    • @Stefan Steinegger 是的,你是对的。有趣的是我以前没有看到。我修正了我的答案以适应这一点。
    • 嗯,签名不匹配。 Its Count(Expression> 表达式);` 而不是Count&lt;T&gt;(Expression&lt;Func&lt;T, object&gt;&gt; expression);
    猜你喜欢
    • 1970-01-01
    • 2020-03-09
    • 2014-08-31
    • 1970-01-01
    • 2011-06-28
    • 2017-02-06
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    相关资源
    最近更新 更多