Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is using the <code>NHibernate.LambdaExtensions</code> syntax but should be easily transformed into a standard criteria query or hql query.</p> <pre><code>var query1 = DetachedCriteria.For&lt;Post&gt;() .CreateCriteria&lt;Post&gt;(x =&gt; x.Comments) .Add&lt;Comment&gt;(x =&gt; x.CreatedDate &gt;= DateTime.Now.AddDays(-5)); query1.GetExecutableCriteria(session).List&lt;T&gt;(); </code></pre> <p>You will also probably want to eagerly load the collection and set your limits to this applicable to what you want to avoid pulling the whole database back and the N+1 query condition of iterating over a lazily loaded list.</p> <p>The first step to be solving any type of complex query with NHibernate is to start from the original SQL so really we need to start with something like</p> <pre><code>Select P.* From Post P Where P.PostID Exists ( Select P1.PostID From Posts P1 Inner Join Comments C ON ( P1.PostID = C.PostID ) Where P1.PostID Exists ( Select Top 5 C1.PostID, Count(*) as PostCount From Comments C1 Group By C1.PostID Order By PostCount DESC ) And C.CreateDate &gt; Now - 5 days ) </code></pre> <p>This was written by hand so this is more an approximation of the query and should be able to point you in the direction of being able to solve the query. With some feedback on this I can add more on translating it to NH. Also that last outer exists might be able to be dropped in the final NH version because I believe that matrix of Post with Comments is how NH will eagerly load the entities.</p>
 

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