Note that there are some explanatory texts on larger screens.

plurals
  1. PONhibernate Subquery - Could not resolve property of type x
    text
    copied!<p>I need some help figuring out a query which should filter based on one to many relationship. I have a tables 'Product' and 'ProductType'. Where 'Product' has many 'ProductType'. These models are working great I just can't figure out how to filter 'Product' records based on 'ProductType' records.</p> <p>In this example I would like to select all a 'Product' where Product.Name == "somename" &amp;&amp; Product.Version == "9.6". I want to filter these results where ProductType.Type == "someType". The following is throwing an exception: </p> <blockquote> <p>could not resolve property: ProductType of: my.namespace.ProductType</p> </blockquote> <p>I am using examples from Ayende's blog: <a href="http://ayende.com/blog/4023/nhibernate-queries-examples" rel="nofollow">Query Examples</a> </p> <pre><code> var product = _session.CreateCriteria&lt;Product&gt;() .Add(Restrictions.Eq("Name", "somename")) .Add(Restrictions.Eq("Version", "9.6")) .Add(Subqueries.PropertyIn("Id", DetachedCriteria.For&lt;ProductType&gt;() .SetProjection(Projections.Property("Product.Id")) .CreateCriteria("ProductType") .Add(Restrictions.Eq("Type", "someType")) )).List&lt;Product&gt;().SingleOrDefault(); </code></pre> <p>Am I close? Can someone offer me some help? Thanks!</p> <p><strong>Edit</strong></p> <p>This has me close. If I remove the second CreateCriteria I get back a product that has the Types populated. As soon as I add the join back.. I get 0 results.</p> <pre><code> var products = _session.CreateCriteria("Product") .Add(Restrictions.Eq("Name", "somename")) .Add(Restrictions.Eq("Version", "9.6")) //This works .CreateCriteria("Types", "productTypes", JoinType.LeftOuterJoin) .Add(Restrictions.Eq("Type", "typename")).List&lt;Product&gt;(); return products.FirstOrDefault(); </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