如何使用标准 api 在 nhibernate 中按范围进行分组

发布于 2024-10-15 06:25:10 字数 539 浏览 3 评论 0原文

我想在 Criteria api 中编写 sql:

SELECT CASE 
WHEN total_pages = 0 THEN '0 pages'
WHEN total_pages BETWEEN 1 AND 3 THEN '1-3 pages'
WHEN total_pages BETWEEN 4 AND 6 THEN '4-6 pages'
WHEN total_pages > 6 THEN '7 + pages'
END AS pagerange, sum(total_pages) AS 'Total'
FROM dbo.connector_log_entry
GROUP BY 
CASE 
WHEN total_pages = 0 THEN '0 pages'
WHEN total_pages BETWEEN 1 AND 3 THEN '1-3 pages'
WHEN total_pages BETWEEN 4 AND 6 THEN '4-6 pages'
WHEN total_pages > 6 THEN '7 + pages'
END
ORDER BY pagerange

I have sql that i would like to write in Criteria api:

SELECT CASE 
WHEN total_pages = 0 THEN '0 pages'
WHEN total_pages BETWEEN 1 AND 3 THEN '1-3 pages'
WHEN total_pages BETWEEN 4 AND 6 THEN '4-6 pages'
WHEN total_pages > 6 THEN '7 + pages'
END AS pagerange, sum(total_pages) AS 'Total'
FROM dbo.connector_log_entry
GROUP BY 
CASE 
WHEN total_pages = 0 THEN '0 pages'
WHEN total_pages BETWEEN 1 AND 3 THEN '1-3 pages'
WHEN total_pages BETWEEN 4 AND 6 THEN '4-6 pages'
WHEN total_pages > 6 THEN '7 + pages'
END
ORDER BY pagerange

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

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

发布评论

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

评论(1

北方的巷 2024-10-22 06:25:10
var groupBy = @"
    CASE
      WHEN total_pages = 0 THEN '0 pages'
      WHEN total_pages BETWEEN 1 AND 3 THEN '1-3 pages'
      WHEN total_pages BETWEEN 4 AND 6 THEN '4-6 pages'
      WHEN total_pages > 6 THEN '7 + pages'
    END";
var pagerange = Projections.SqlGroupProjection(groupBy + " pagerange",
                                               groupBy,
                                               new[] { "pagerange" },
                                               new[] { NHibernateUtil.String });
var results = session.CreateCriteria<ConnectorLogEntry>()
                     .SetProjection(pagerange,
                                    Projections.Sum("TotalPages"))
                     .List();

假设您的类名为 ConnectorLogEntry,并且映射到total_pages 列的属性名为 TotalPages

我欠你 ORDER BY 位,但你总是可以在客户端排序。

var groupBy = @"
    CASE
      WHEN total_pages = 0 THEN '0 pages'
      WHEN total_pages BETWEEN 1 AND 3 THEN '1-3 pages'
      WHEN total_pages BETWEEN 4 AND 6 THEN '4-6 pages'
      WHEN total_pages > 6 THEN '7 + pages'
    END";
var pagerange = Projections.SqlGroupProjection(groupBy + " pagerange",
                                               groupBy,
                                               new[] { "pagerange" },
                                               new[] { NHibernateUtil.String });
var results = session.CreateCriteria<ConnectorLogEntry>()
                     .SetProjection(pagerange,
                                    Projections.Sum("TotalPages"))
                     .List();

This assumes your class is called ConnectorLogEntry and the property mapped to the total_pages column is called TotalPages.

I owe you the ORDER BY bit, but you can always sort client-side.

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