Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've managed to resolve a similar problem by slightly adapting the original sql query. I've ended up with something like this (pseudo sql code):</p> <pre><code>SELECT p.* FROM [Items] as p WHERE EXISTS ( SELECT [childId] as childNodeId FROM [Items] as q WHERE p.[childId] = q.[childNodeId] GROUP BY q.[childId] HAVING p.[price] = MAX(q.[price]) ) </code></pre> <p>And this is the QueryOver implementation:</p> <pre><code>var subquery = QueryOver.Of(() =&gt; q) .SelectList(list =&gt; list.SelectGroup(() =&gt; q.ChildId)) .Where(Restrictions.EqProperty( Projections.Property(() =&gt; p.Price), Projections.Max(() =&gt; q.Price))) .And(Restrictions.EqProperty( Projections.Property(() =&gt; p.ChildId), Projections.Property(() =&gt; q.ChildId))); </code></pre> <p>From here you only need to pass the aliases so that NHibernate can resolve entities correctly (pseudo code):</p> <pre><code>var filter = QueryOver.Of(() =&gt; p) .WithSubquery.WhereExists(GetSubQuery(p, criteria...)); </code></pre> <p>I hope this helps in your particular case.</p> <p><strong>UPDATE:</strong> Criteria API</p> <pre><code>var subquery = DetachedCriteria.For&lt;Items&gt;("q") .SetProjection(Projections.ProjectionList() .Add(Projections.GroupProperty("q.ChildId"))) .Add(Restrictions.EqProperty("p.Price", Projections.Max("q.Price"))) .Add(Restrictions.EqProperty("p.ChildId", "q.ChildId")); var query = DetachedCriteria.For&lt;Items&gt;("p") .Add(Subqueries.Exists(subquery)); </code></pre> <p>Nevertheless I would recommend sticking to the <code>QueryOver</code> version, it's much more intuitive and you avoid magic strings (especially that you don't have to upgrade the NH version).</p> <p>Please let me know if this is working for you.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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