按可空整数对 NHibernate 查询结果进行分组

发布于 2024-12-01 21:24:05 字数 1072 浏览 0 评论 0原文

我正在使用 NHibernate 查询一个表,该表具有以下格式的元组:(String, Int?),其中整数可以为空。因此,我想按数字对结果进行分组,然后按字母顺序排序。在获得查询结果后,我可以轻松地执行此操作,但我想让 NHibernate 制定一个执行此操作的查询。这是我想要的结果示例:

alpha, 1
三角洲,4
高尔夫,3
酒店, 2
利马, 5
查理,0
θ, 0
测试版,空
echo, null

我正在寻找的三个分组是:(int > 0)、(int == 0) 和(int = null)。这是我正在使用的查询:

var devices = session.QueryOver<Table>()
                .OrderBy(item => item.Number).Desc
                .OrderBy(item => item.Name).Asc
                .List();

目前,我在查询完成后对它们进行排序,如下所示:

List<Table> sortedDevices = devices.OrderBy(item => item.Name).Where(item => item.Number > 0).ToList();
            sortedDevices = sortedDevices.Concat(devices.OrderBy(item => item.Name).Where(item => item.Number == 0).ToList()).ToList();
            sortedDevices = sortedDevices.Concat(devices.OrderBy(item => item.Name).Where(item => item.Number == null).ToList()).ToList();

Is it possible to get NHibernate to group query like this?

I'm using NHibernate to query a table that has tuples in the format: (String, Int?), where the integers can be null. So, I want to group my results by number and then sort alphabetically. I can easily do this after I get the query results, but I would like to get NHibernate to formulate a query that does it. Here's an example of the results I would like:

alpha, 1
delta, 4
golf, 3
hotel, 2
lima, 5
charlie, 0
theta, 0
beta, null
echo, null

The three groupings I'm looking for are: (int > 0), (int == 0), and (int = null). Here's the query I'm using:

var devices = session.QueryOver<Table>()
                .OrderBy(item => item.Number).Desc
                .OrderBy(item => item.Name).Asc
                .List();

Currently, I'm sorting them after the query is done, as such:

List<Table> sortedDevices = devices.OrderBy(item => item.Name).Where(item => item.Number > 0).ToList();
            sortedDevices = sortedDevices.Concat(devices.OrderBy(item => item.Name).Where(item => item.Number == 0).ToList()).ToList();
            sortedDevices = sortedDevices.Concat(devices.OrderBy(item => item.Name).Where(item => item.Number == null).ToList()).ToList();

Is it possible to get NHibernate to group queries like this?

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

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

发布评论

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

评论(1

戏舞 2024-12-08 21:24:05

类似的事情:

session.QueryOver<User>()
    .Select(Projections.Alias(Projections
        .Conditional(Expression.Gt("Number", 0),
            Projections.Constant(1),
            Projections.Conditional(Expression.Eq("Number", 0),
                Projections.Constant(0),
                Projections.Constant(-1))),
        "group"))
    .OrderBy(Projections.Property("group")).Desc
    .ThenBy(table => table.Name).Asc
    .List();

something along the lines:

session.QueryOver<User>()
    .Select(Projections.Alias(Projections
        .Conditional(Expression.Gt("Number", 0),
            Projections.Constant(1),
            Projections.Conditional(Expression.Eq("Number", 0),
                Projections.Constant(0),
                Projections.Constant(-1))),
        "group"))
    .OrderBy(Projections.Property("group")).Desc
    .ThenBy(table => table.Name).Asc
    .List();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文