Note that there are some explanatory texts on larger screens.

plurals
  1. POHowto use predicates in LINQ to Entities for Entity Framework objects
    primarykey
    data
    text
    <p>I'm using LINQ to Entities for Entity Framework objects in my Data Access Layer.</p> <p>My goal is to filter as much as I can from the database, without applying filtering logic to in-memory results.</p> <p>For that purpose Business Logic Layer passes a predicate to Data Access Layer. </p> <p>I mean</p> <pre><code>Func&lt;MyEntity, bool&gt; </code></pre> <p>So, if I use this predicate directly, like</p> <pre><code>public IQueryable&lt;MyEntity&gt; GetAllMatchedEntities(Func&lt;MyEntity, Boolean&gt; isMatched) { return qry = _Context.MyEntities.Where(x =&gt; isMatched(x)); } </code></pre> <p>I'm getting the exception </p> <blockquote> <p>[System.NotSupportedException] --- {"The LINQ expression node type 'Invoke' is not supported in LINQ to Entities."}</p> </blockquote> <p>Solution in <a href="https://stackoverflow.com/questions/8741667/the-linq-expression-node-type-invoke-is-not-supported-in-linq-to-entities-in-e"><strong>that question</strong></a> suggests to use AsExpandable() method from <a href="http://www.albahari.com/nutshell/linqkit.aspx" rel="nofollow noreferrer"><strong>LINQKit</strong></a> library.</p> <p>But again, using</p> <pre><code>public IQueryable&lt;MyEntity&gt; GetAllMatchedEntities(Func&lt;MyEntity, Boolean&gt; isMatched) { return qry = _Context.MyEntities.AsExpandable().Where(x =&gt; isMatched(x)); } </code></pre> <p>I'm getting the exception</p> <blockquote> <p>Unable to cast object of type 'System.Linq.Expressions.FieldExpression' to type 'System.Linq.Expressions.LambdaExpression'</p> </blockquote> <p>Is there way to use predicate in LINQ to Entities query for Entity Framework objects, so that it is correctly transformed it into a SQL statement.</p> <p>Thank 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.
 

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