Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When quering over multiple child collections using NHibernate, I tend to prefer to use the LINQ query syntax over lambdas because not only is it much easier to write but the code looks cleaner.</p> <p>Take this example which should point you in the right direction:</p> <pre><code>var listOfString = new List&lt;string&gt;() { "String1", "String2" }; var customers = ( from a in session.Query&lt;A&gt;() from b in a.B from c in b.C where a.Status == "Active" &amp;&amp; listOfStrings.Contains( b.SomeField ) &amp;&amp; listOfStrings.Contains( c.someOtherField ) select a ) .ToList(); </code></pre> <p>By using the LINQ query syntax, we can easily add multiple FROMs to gain access to the child collections. Then it's just a matter of apply your WHERE conditions. For your particular case, you would simply use LINQ's Contains() method which equates to SQL's IN operator.</p> <p>It also sounds like you are wanting to load all the child collections into memory as well so be sure to use NHibernate's .Fetch() method to eagerly load that data like so:</p> <pre><code>var customers = ( from a in session.Query&lt;A&gt;() from b in a.B from c in b.C where a.Status == "Active" &amp;&amp; listOfStrings.Contains( b.SomeField ) &amp;&amp; listOfStrings.Contains( c.someOtherField ) select a ) .Fetch( x =&gt; x.B ) .ThenFetchMany( x =&gt; x.First().C ) .ToList(); </code></pre> <p>FYI, using the .First() inside the .ThenFetchMany() method above is a trick I learned from the NHibernate's Jira issue tracker website: <a href="https://nhibernate.jira.com/browse/NH-2972?focusedCommentId=22150&amp;page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-22150" rel="nofollow">https://nhibernate.jira.com/browse/NH-2972?focusedCommentId=22150&amp;page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-22150</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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