3

从昨天早上开始,我一直在与这个查询作斗争,但找不到解决方案。我想要做的是有一个像这样的查询:

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

有人知道怎么做吗?Childs 是一个 HasMany-Collection。

问候,马丁

4

1 回答 1

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)
于 2013-07-05T12:03:46.563 回答