c# - linq lambda expression for sql contains -


i using generic repository, this:

 itemslist = (from myrow in uow.filerepository.get()  select new filemodel()  {record_id = myrow.type_id,   descr = myrow.descr}).tolist();}); 

and method:

 public virtual ienumerable<tentity> get()     {         //  _aquery = _thedbcontext.set<tentity>();         ienumerable<tentity> query = _aquery;         return query;     } 

how implement generic linq lambda expression if wanted create similar query search particular string in particular field? in viewmodel call like:

     myrow in uow.filerepository.srch(nameoffieldtosearch, searchstring).  

the query this?

   public ienumerable<tentity> srch(expression<func<tentity, bool>> expression)     {         ienumerable<tentity> srchlist = _aquery.tolist();         return srchlist.where(????);      } 

thank suggestions.

edit----------------------- have queries , srch in general repository class , need know how declare query in repository class , how call search string viewmodel. not sure if there consensus where/when materialize , compile? saw discussion http://www.fascinatedwithsoftware.com/blog/post/2012/01/10/more-on-expression-vs-func-with-entity-framework.aspx , quote below inquire whether same approach being suggested here? thank again.

"the profiler told loadmyentities being called many, many times , taking large fraction of our cpu time. simple change below solved problem. can guess why?"

public ienumerable<myentity> loadmyentities(func<myentity, bool> predicate) {return context.myentities.where(predicate);} 

"the parameter func<> instead of expression>. reason makes difference predicate that's in form of expression passed sql server, predicate that's passed func not. normally, you'd want sql server possible, , expression right choice, in case we'd pre-load entire table in context -- func do.

  1. the extension method has 2 flavors. 1 extends iqueryable , takes expression parameter. other extends ienumerable , takes func.
  2. because 'predicate' func, extends ienumerable used.
  3. the entity framework's fluent interface constructing sql queries based on iqueryables, not ienumerables. therefore, fluency stops before where. part of statement gets passed entity framework context.myentities.
  4. context.myentities therefore returns entire table context.
  5. the entire table filtered predicate, , value want returned.
  6. the next time method called, entity framework realizes record want in context. (in case, querying primary key, , ef apparently smart enough know if there's record in context id, won't find additional such record in database.) since don't go out sql server, save lots of time. there occasions when not want this, in our case wanted. table relatively small, , same context queried hundreds of times.

in original version, predicate expression, compiler used extends iqueryable. predicate passed sql server, dutifully returned 1 row context. next time called loadmyentities, entity framework had call sql server again."

this simple implementation using expression trees. following complete solution:

method fetch expression srch method:

public expression<func<tentity, bool>> srchexpression(string nameoffieldtosearch, string searchstring) {   var parametertype = expression.parameter(typeof(tentity), "obj");    var memberexpression = expression.property(typeof(string), nameoffieldtosearch)   // calls extension method created underneath  var filtersmethodinfo = typeof(stringextensions).getmethod("contains", new[] { typeof(string), typeof(string) });   var filtersconstantexpression = expression.constant(searchstring, typeof(string));   var finalexpression = expression.call(null, filtersmethodinfo, memberexpression, filtersconstantexpression)   return expression.lambda<func<tentity, bool>>(finalexpression, parametertype) } 

// create string extension method contains

public static class stringextensions {             public static bool contains(this string source, string searchstring)     {         return source?.indexof(substring, stringcomparison.ordinalignorecase) >= 0;     } } 

now srch method shall like:

public ienumerable<tentity> srch(expression<func<tentity, bool>> expression) {     func<tentity, bool>> func = expression.compile();     ienumerable<tentity> srchlist = _aquery.where(o => func(o));     return srchlist; } 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo