Nhibernate QueryOver获取少于3个孩子的所有父母

时间:2013-07-03 06:55:43

标签: nhibernate fluent-nhibernate queryover nhibernate-criteria

我从昨天早上开始就遇到了这个问题而无法找到解决方案。我想要做的是查询:

IList<Parent> = _session.QueryOver<Parent>()
   .Where(Restrictions.lt(x => x.Childs.Count,4))
   .List<Parent>();

任何人都知道如何做到这一点? Childs是一个HasMany-Collection。

此致 马丁

1 个答案:

答案 0 :(得分:2)

这是使用子查询执行此操作的一种方法:

Parent parentAlias = null;

IList<Parent> = _session.QueryOver<Parent>(() => parentAlias)
    .WithSubquery.WhereValue(4).Gt(
        QueryOver.Of<Child>()
            .Where(ch => ch.Parent.Id == parentAlias.Id)
            .Select(Projections.Count<Child>(ch => ch.Id)
        )
    .List<Parent>();

这将生成如下内容:

SELECT this_.Id
       /* select list continues */
FROM   [Parent] this_
WHERE  4 /* @p0 */ > (SELECT count(this_0_.Id) as y0_
              FROM   [Child] this_0_
              WHERE  this_0_.ParentId = this_.Id)
相关问题