NHibernate: Criteria expression to retrieve all entities with null count child collection

你。 提交于 2019-12-21 06:29:47

问题


In nhibernate, I have two classes that are associated with a many-to-one mapping:

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

I would like to use a criteria expression to get only Employees where the the collection is null (ie no orgunits) , something like this :

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

This doesn't filter the collection as I expect.


回答1:


Colleague just found one way that works.

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


来源:https://stackoverflow.com/questions/942905/nhibernate-criteria-expression-to-retrieve-all-entities-with-null-count-child-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!