Search code examples
nhibernatenhibernate-criteria

NHibernate filter collection and parent


I have an object "Owner" with collection property "Cars". I want to filter Owners based on Cars criteria (Ex: All Owners with Red Cars) and I dont want to query non red cars for SomeOwner.Cars. So I want one query filter for both parent and collection.

<class name="Owner"> 
    <set name="Cars">
      <key column="FK_Owner" />
      <one-to-many class="Car" />
    </set>
</class>

<class name="Car"> 
    <many-to-one name="Owner" column="FK_Owner" />
</class>

How can I do that?


Solution

  •  Car carAlias = null;
     var cars = _session.QueryOver<Owner>()
       .JoinAlias(x=>Cars,()=>carAlias,JoinType.LeftOuterJoin)    
       .Where(x=>carAlias.Color=="Red")
       .List();