c# - How can I make Linq extension method available for the entities? -


i wrote method extends system.linq class. containsanyword method allows me search words in string against string instead of comparing 1 string another.

here method wrote extend linq

public static class linqextension {      public static bool containsanyword(this string value, string searchfor)     {         if (!string.isnullorwhitespace(value) && !string.isnullorwhitespace(searchfor))         {             value = value.tolower();              ienumerable<string> tags = stopwordtool.getpossibletags(searchfor);              return tags.contains(value);          }          return false;      }  } 

this extension method works great on ienumerable object. when want use on iqueryable object following error

linq entities not recognize method 'boolean containsanyword(system.string, system.string)' method, , method cannot translated store expression.

the following example works great because working list.

using(var conn = new appcontext()) {      var allusers = conn.users.getall().tolist();     var foundusers = allusers.where     (         user =>                 user.firstname.containsanyword("some full name goes here")             || user.lastname.containsanyword("some full name goes here")     ).tolist();  } 

but following example not work because working iqueryable gives me error listed above.

using(var conn = new appcontext()) {      var allusers = conn.users.where     (         user =>              user.firstname.containsanyword("some full name goes here")           || user.lastname.containsanyword("some full name goes here")     ).tolist();  } 

how can fix issue , make method available iqueryable object?

updated

based on feedback got below, tried implement using iqueryable so

    public static iqueryable<t> containsanyword<t>(this iqueryable<t> query, string searchfor)     {         if (!string.isnullorwhitespace(searchfor))         {             ienumerable<string> tags = stopwordtool.getpossibletags(searchfor);              return query.where(item => tags.contains(item));          }          return query;      } 

but giving me error

enter image description here

you have keep in mind has translated sql @ end. containsanyword cannot translated sql...
,save names in list/array , try

 user =>  yourlist.contains(  user.firstname) 

ef translate ..in

there no need method if reason want it

internal static class myclass2 {     public static iqueryable<t> containsanyword<t>(this iqueryable<t> value, string searchfor) t: user     {         var names = searchfor.split(' ').tolist();         return value.where(u => names.contains(u.displayname));     } } 

you can use like

var result=conn.users.containsanyword("abc def ght"); 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -