Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate - filtering out results based on child-property
    text
    copied!<p>I have this code fetching all enabled Groups with their children. The problem I have is that the children can also be disabled but I can't get fluent nhibernate to only fetch groups where <strong>all</strong> childrens are enabled. I assume this is possible but how?</p> <pre><code>public class Group { public bool IsDisabled { get; set; } public string Description { get; set; } public ICollection&lt;ChildType&gt; Children { get; protected set; } } public class ChildType { public bool IsDisabled { get; set; } public string Description { get; set; } } public IList&lt;Group&gt; Search(string searchString) { IQueryOver&lt;Group&gt; query = Session.QueryOver&lt;Group&gt;() .WhereRestrictionOn(x =&gt; x.Description).IsInsensitiveLike(searchString, MatchMode.Start) .Where(x =&gt; !x.IsDisabled) .OrderBy(x =&gt; x.Description).Asc .Fetch(group =&gt; group.Children).Eager; return query .Cacheable() .List(); } </code></pre> <p><strong>Edit:</strong> There is a N:M-relation between children and groups.</p> <p>The following is the solution I used:</p> <pre><code>public class Group { public long Id { get; set; } public bool IsDisabled { get; set; } public string Description { get; set; } public ICollection&lt;ChildType&gt; Children { get; protected set; } } public class ChildType { public long Id { get; set; } public bool IsDisabled { get; set; } public string Description { get; set; } public ICollection&lt;Group&gt; Groups { get; protected set; } } public IList&lt;Group&gt; Search(string searchString) { ChildType child = null; Group group = null; Group joinedGroup = null; var notDisabled = Session.QueryOver.Of&lt;ExaminationType&gt;() .Where(x =&gt; x.IsDisabled) .JoinAlias(x =&gt; x.Groups, () =&gt; joinedGroup ) .Where(x =&gt; joinedGroup == group) .Select(x =&gt; x.Id); IQueryOver&lt;Group&gt; query = Session.QueryOver&lt;Group&gt;() .WhereRestrictionOn(x =&gt; x.Description).IsInsensitiveLike(searchString, MatchMode.Start) .JoinAlias(x =&gt; x.ExaminationTypes, () =&gt; child) .WithSubquery.WhereNotExists(notDisabled) .OrderBy(x =&gt; x.Description).Asc; return query .Cacheable() .List(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload