我有一个HQL查询,它返回实体的投影,包括某个子集合的计数。
select t.Id as TagId, t.Name as Name, count(elements(t.Documents)) as Count
from Tag t
group by t.Id, t.Name, t.User
having t.User.Id = :userId"
除了我需要在计数上放置一个条件外,它有效。我只想计算Trashed = false的文档。
我似乎对如何做到这一点有心理障碍。我正在使用NHibernate 3.0。我对ICriteria或Linq查询同样满意。
更新:以下是完全正确的查询:
select t.Id as TagId, t.Name as Name, sum(case when d.Trashed = false then 1 else 0 end) as Count
from Tag t
left join t.Documents d
group by t.Id, t.Name, t.User
having t.User.Id = :userId and sum(case when d.Trashed = false then 1 else 0 end) > 0
答案 0 :(得分:3)
sum(case when d.Trashed = false then 1 else 0 end) as Count