Note that there are some explanatory texts on larger screens.

plurals
  1. POOften used LINQ returned from method
    primarykey
    data
    text
    <p>I have 1 LINQ which used so much. I try create the method which return this LINQ like:</p> <pre><code> public static System.Linq.Expressions.Expression&lt;Func&lt;MyEntity, bool&gt;&gt; GetFilteredEntity() { return x =&gt; true/*Some condition*/; } public static Func&lt;MyEntity, bool&gt; GetFilteredEntity() { return x =&gt; true/*Some condition*/; } </code></pre> <p>And use this like </p> <pre><code> db.MyEntities.Where(GetFilteredEntity()); </code></pre> <p>is successfull, But! I need use it like</p> <pre><code> db.ParentEntities.Where(entity =&gt; entity.MyEntities.Where(GetFilteredEntity())); </code></pre> <p>This code compiled too, but every time when i use it, i got the Error:</p> <pre><code>System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first. </code></pre> <p>,even:</p> <pre><code>db.ParentEntities.Where(entity =&gt; entity.MyEntities.Where(GetFilteredEntity())).ToList(); </code></pre> <p>throw this Exception too.</p> <p>But,</p> <pre><code>db.ParentEntities.Where(entity =&gt; entity.MyEntities.Where(x =&gt; true/*Some condition*/)) </code></pre> <p>still works fine! So why it happend, and have some way to get round this?</p> <p><strong>Final working code</strong></p> <p><code>public static Expression&lt;Func&lt;MyEntity, bool&gt;&gt; GetFilteredEntity() { return x =&gt; true/*Some condition*/; }</code></p> <p>and</p> <p><code>var expression = GetFilteredEntity();</code></p> <p><code>db.ParentEntities.Where(entity =&gt; entity.MyEntities.AsQueryable().Where(expression ));</code></p> <p>Also <code>.AsQueryable()</code> thanks to <a href="https://stackoverflow.com/questions/1711448/passing-func-as-parameter-in-linq-to-entities-and-internal-net-framework-data">Passing func as parameter in Linq to Entities and &#39;Internal .NET Framework Data Provider error 1025&#39; error</a></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.
 

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