您可以使用 QueryOver 获得所需的结果,尽管由于子查询的原因它会变慢。
var sums = repo.Session.QueryOver<Tutorials>()
.SelectList(list => list
.SelectSubQuery<Tutorials>(NHibernate.Criterion.QueryOver.Of<Tutorials>()
.Where(t => t.IsWithdrawn)
.ToRowCountQuery())
.SelectSubQuery<Tutorials>(NHibernate.Criterion.QueryOver.Of<Tutorials>()
.ToRowCountQuery())
)
.Take(1) // we want only one row in our result. In SQL I would use " from dummy".
.List<object[]>();
解释:
我使用两个分离的 QueryOver。第一个计算教程中 IsWithdrawn = true 的行,第二个计算所有行。然后将两个分离的 QueryOver 用作带有 Projection (SelectList) 的普通 QueryOver 中的子查询。
这是生成的 SQL:
SELECT TOP (1)
(SELECT count(*) as y0_ FROM [Tutorials] this_0_
WHERE this_0_.IsWithdrawn = True) as y0_,
(SELECT count(*) as y0_ FROM [Tutorials] this_0_) as y1_
FROM [Tutorials] this_;