NHibernate:Criteria表达式,用于检索具有空计数子集合的所有实体

时间:2009-06-03 03:14:53

标签: nhibernate criteria

在nhibernate中,我有两个与多对一映射关联的类:

<class name="Employee" table="Employee">
  ..
  <bag name="orgUnits">
    <key column="id" />
    <one-to-many name="OrgUnit" class="OrgUnit">
  </bag>
  ..
</class>

我想使用条件表达式来仅获取集合为空的Employees(即没有orgunits),如下所示:

IList employeesWithNoOrgUnit = sess.CreateCriteria(typeof(Employee))
    .Add( Expression.IsNull("OrgUnits") )
    .List();

这不会像我期望的那样过滤集合。

1 个答案:

答案 0 :(得分:5)

同事刚刚发现了一种有效的方式。

IList employeesWithNoOrgUnit = sess.CreateCriteria(typeof(Employee))
    .Add( Restrictions.IsEmpty("OrgUnits") )
    .List();
相关问题