0

I'm trying to use generics to search on some Entities using search parameters defined in a int/string Dictionary. I have a search info class like:

 public class SearchInfo<TEntity, TComparer>{
     public Func<TEntity, TComparer> Search { get; set; }
     public Func<string, TComparer> ConvertValueToSearchType { get; set; }
 }

Say i'm searching on a Int64 my Search Info will be set up like:

var searchInfo = new SearchInfo<Entity, Int64>();
searchInfo.Search = new Func<Entity, Int64>(i => i.Child.Id);
searchInfo.ConvertValueToSearchType  = new Func<string, Int64>(s=>Int64.Parse(s));

I'd like to be able to do

items = items.Where(i=> searchInfo.Search(i) == searchInfo.ConvertValueToSearchType(dictionary.Value));

However I keep getting the exception

An exception of type 'System.NotSupportedException' occurred in mscorlib.dll but was not handled in user code

Additional information: The LINQ expression node type 'Invoke' is not supported in LINQ to Entities.

Anyone know how I can accomplish this?

xanatos
  • 109,618
  • 12
  • 197
  • 280
DParry
  • 952
  • 9
  • 15
  • LINQ to entities will have no clue what to do with a `Func<...>`, always use `Expression>` – DavidG Mar 30 '17 at 13:10
  • 1
    ...and even then, I don't think the EF provider will have any success in translating that sort of thing to SQL. You're either going to have to do this filtering in memory or come up with a different approach. – Charles Mager Mar 30 '17 at 13:11

0 Answers0