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

Jaf*_*fin 2 nhibernate criteria

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

<class name="Employee" table="Employee">
  ..
  <bag name="orgUnits">
    <key column="id" />
    <one-to-many name="OrgUnit" class="OrgUnit">
  </bag>
  ..
</class>
Run Code Online (Sandbox Code Playgroud)

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

IList employeesWithNoOrgUnit = sess.CreateCriteria(typeof(Employee))
    .Add( Expression.IsNull("OrgUnits") )
    .List();
Run Code Online (Sandbox Code Playgroud)

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

Jaf*_*fin 5

同事刚刚发现了一种有效的方法.

IList employeesWithNoOrgUnit = sess.CreateCriteria(typeof(Employee))
    .Add( Restrictions.IsEmpty("OrgUnits") )
    .List();
Run Code Online (Sandbox Code Playgroud)