Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"The LINQ expression node type 'Invoke' is not supported in LINQ to Entities" - stumped!

In my EF later, I'm trying to pass in an anonymous function to be used as part of my Linq query. The function would pass in an INT and return a BOOL (u.RelationTypeId is an INT). Below is a simplified version of my function:

public IEnumerable<UserBandRelation> GetBandRelationsByUser(Func<int, bool> relation) {     using (var ctx = new OpenGroovesEntities())     {         Expression<Func<UsersBand, bool>> predicate = (u) => relation(u.RelationTypeId);          var relations = ctx.UsersBands.Where(predicate);          // mapping, other stuff, back to business layer         return relations.ToList();     } } 

However, I get the error stated above. It seems like I'm going everything correct by building a predicate from the function. Any ideas? Thanks.

like image 629
Ryan Peters Avatar asked Mar 12 '11 19:03

Ryan Peters


1 Answers

I was getting this very error and I'm using Entity Framework with PredicateBuilder by Joe Albahari to build dynamic where clauses. If you happen to be in the same condition, you should call the AsExpandable method:

If querying with Entity Framework, change the last line to this:

return objectContext.Products.AsExpandable().Where(predicate);

This method is part of LINQKIT DLL that you can grab here or through a NuGet package here.

Everything works fine now. :)

like image 191
Leniel Maccaferri Avatar answered Sep 20 '22 16:09

Leniel Maccaferri