使用nhibernate得到关系计数大于x的entites

时间:2009-07-01 21:51:37

标签: nhibernate

说我有这样的实体

public class Something
{
    public int Id{get;set;}
    public string Name{get;set;}
    public IList<SomeOther> Assosiation{get;set}
}

如何使用nhibernate查询使用Criteria API获取超过10个Assosiations的所有Something实体?

干杯 科林

1 个答案:

答案 0 :(得分:4)

这样的事情:

var dc = DetachedCriteria.For<SomeOther>()
    .SetProjection(Projections.GroupProperty("Something.Id"))
    .Add(Restrictions.Gt(Projections.RowCount(), 10));

var criteria = session.CreateCriteria(typeof (Something))
    .Add(Subqueries.PropertyIn("Id", dc));

哪会产生这样的东西:

SELECT this_.Id             as Id7_0_,
       this_.Title          as Title7_0_
FROM   Something this_
WHERE  this_.Id in (SELECT   this_0_.SomethingId as y0_
                    FROM     SomeOther this_0_
                    GROUP BY this_0_.SomethingId
                    HAVING   count(* ) > 10 /* @p0 */)