使用 Linq (nHibernate) 执行带有 Counts 的 Case 语句
我确信这个房子已经做过几次了,但我从未找到解决方案...
那么是否可以使用 nHibernate 3 最好使用 Linq 来做这样的事情:
SELECT
COUNT(CASE WHEN IsWithdrawn = 1 THEN 1 END) AS WithdrawnCount,
COUNT(CASE WHEN IsWithdrawn = 0 THEN 1 END) AS ViewAllCount
FROM Tutorials
我很确定这不是'不可能,最好的解决方案是在这种情况下只选择 sql...但也许 nHibernate 3.1 中有一些新的东西可以做到这一点,甚至使用 queryover ?
谢谢
I'm sure this is one has done the houses a few times but i've never found a solution...
So is it possible to do something like this using nHibernate 3 with preferably Linq:
SELECT
COUNT(CASE WHEN IsWithdrawn = 1 THEN 1 END) AS WithdrawnCount,
COUNT(CASE WHEN IsWithdrawn = 0 THEN 1 END) AS ViewAllCount
FROM Tutorials
I'm pretty sure that is isn't possible and that the best solution is to opt just for sql in this case... but maybe there is something new in nHibernate 3.1 that can do this with, even using queryover?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 HQL 来完成此操作,这与 SQL 几乎相同:(
我不确定 COUNT 是否可以工作,我确信 SUM 可以)
这是一个也应该可以工作的 LINQ 版本:
您可以使用
Projections .Conditional
使用 Criteria 或 QueryOver,但需要更多工作。You can do it with HQL, which is almost the same as SQL:
(I'm not sure if the COUNT would work, I'm sure SUM does)
Here's a LINQ version that should work too:
You can use
Projections.Conditional
with Criteria or QueryOver, but it's more work.您可以使用 QueryOver 获得所需的结果,尽管由于子查询而速度会较慢。
说明:
我使用两个独立的 QueryOver。第一个对教程中 IsWithdrawn = true 的行进行计数,第二个对所有行进行计数。然后,这两个分离的 QueryOver 用作带有投影 (SelectList) 的普通 QueryOver 中的子查询。
这是生成的 SQL:
You can get the desired result with QueryOver, although it will be slower due to the subqueries.
Explanation:
I use two detached QueryOvers. The first one counts the rows in Tutorials where IsWithdrawn = true, the second once counts all rows. The two detached QueryOvers are then used as SubQueries in a normal QueryOver with a Projection (SelectList).
Here is the generated SQL: