Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate + LINQ - efficient work with related collections
    primarykey
    data
    text
    <p>I'm quite new to LINQ, but I already know the basic difference between IQueryable and IEnumerable (i.e. conditions applied to the first one are likely to be executed by the remote engine, in my case - translated to SQL).</p> <p>Now, all the NHibernate tutorials I've ever seen use <code>IList&lt;T&gt;</code> for related collections. I'd like to know if there's any way to use <code>IQueryable</code> instead, so the filtering could be efficient.</p> <p>Unfortunately, if I try to use <code>IQueryable&lt;T&gt;</code> as the property type for related collection, it causes an error:</p> <blockquote> <p>Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericBag<code>1[....PersonalDocument]' to type 'System.Linq.IQueryable</code></p> </blockquote> <p>Therefore to keep the filtering of related collections SQL-side, I need to query the related type directly providing the extra condition which "binds" the relation. This is quite inconvenient, and I'd like to avoid this (since the relationship condition is already declared ...)</p> <p>To clarify a bit:</p> <p>I have two models, as follows: </p> <pre><code>public partial class Person { public virtual int Id { get; set; } // .... public virtual IEnumerable&lt;PersonalDocument&gt; Documents { get; set; } } public class PersonalDocument { // ... public virtual int Id { get; set; } public virtual Person Owner { get; set; } public virtual string Type { get; set; } } </code></pre> <p>and the mapping code for <code>Person</code>:</p> <pre><code>HasMany&lt;DomainModel.PersonalDocument&gt;(x =&gt; x.Documents) .KeyColumn("Owner_Id") .Inverse() .Cascade.Delete() .AsBag(); </code></pre> <p>and the query code</p> <pre><code>personInstance.Documents.Where(d =&gt; d.Type == "xx").Count() </code></pre> <p>is being translated to non-optimal SQL (no "type" filter applied to SQL, count done client-side) <img src="https://i.stack.imgur.com/gfYm3.png" alt="related"></p> <p>while this one:</p> <pre><code>s.Query&lt;PersonalDocument&gt;().Count(x =&gt; x.Owner == personInstance &amp;&amp; x.Type == "xx"); </code></pre> <p>translates to a nice SQL:</p> <p><img src="https://i.stack.imgur.com/6D3zf.png" alt="enter image description here"></p> <p><strong>is there any way to write DRY LINQ queries like the first one, yet have resulting SQL optimal like the second one?</strong></p>
    singulars
    1. This table or related slice is empty.
    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. 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