Note that there are some explanatory texts on larger screens.

plurals
  1. POReturn class from nested collection using NHibernate
    primarykey
    data
    text
    <p>Doman:</p> <pre><code>class Action Products: IList of class ActionProducts: Category: class Category Products: IList of class Product </code></pre> <p>Now, I want this:</p> <pre><code> var products = from a in Session.Linq&lt;Action&gt;() from ap in a.Products from p in ap.Category.Products where a.Name == name select p; </code></pre> <p>And this Linq actually works but: 1. produces select for all tables instead of only Products 2. produces left outer joins, not inner 3. Distinct() on the query doesn't work (though ToList().Distinct() works).</p> <p>Also can be done with SelectMany(a => a.Products).SelectMany(ap => ap.Category.Products) but it doesn't work at all with current NHibernate.Linq.</p> <p>So I want to use ICriteria. But I can't see how do I return product, not action?</p> <pre><code> ICriteria criteria = Session.CreateCriteria(typeof(Action)) .Add(Expression.Eq("Name", name)) .CreateAlias("Products", "ap") .CreateAlias("ap.Category.Products", "p") .SomehowReturnMeOnly("p"); </code></pre> <p>So how do I SomehowReturnMeOnly("p")? So that I can do</p> <pre><code>return criteria.List&lt;Product&gt;(); </code></pre> <p>which will fail because ICriteria selects Actions, not Products?</p> <p>I may consider HQL but I actually doesn't like string queries... Just for example, here's the HQL that works and produces exactly the SQL that I need:</p> <pre><code> IQuery query = Session.CreateQuery("select distinct p from Action a inner join a.Products as ap inner join ap.Category.Products as p"); return query.List&lt;Product&gt;(); </code></pre>
    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.
 

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