c# - Converting LINQ Extension method to work with L2E -


i working on search page users able use wildcards * in search criteria. wildcard can placed @ beginning, or end of string. since there several fields need applied to, figured extension method best way. code came works, not iqueryable.

public static class helper {     public static ienumerable<string> myextmethod(this ienumerable<string> items, string searchstring)     {         var searchtype = -1;             if(searchstring.indexof("*") == -1) //no wildcard         {             searchtype = 0;         }         else if(searchstring.indexof("*") == 0 && searchstring.lastindexof("*") == searchstring.length - 1)//start , end         {             searchtype = 1;         }         else if(searchstring.indexof("*") == 0)//ends         {             searchtype = 2;         }         else if(searchstring.lastindexof("*") == searchstring.length - 1) //starts         {             searchtype = 3;         }          var search = searchstring.replace("*", "");          foreach(var in items)         {             switch(searchtype)             {                 case 0: yield return i;                 break;                 case 1: if(i.contains(search))                             yield return i;                 break;                 case 2: if(i.endswith(search))                             yield return i;                 break;                 case 3: if(i.startswith(search))                             yield return i;                 break;             }         }     } } 

i use string manipulation , extension methods supported l2e contains, startswith, endswith. can converted work entities? if so, needs done? thanks.

edit: if possible, love able use such:

db.sometable.where(s => s.somefield.myextmethod(somestring)); 

bonus points reference sources.

ef uses iqueryable<t> - not ienumerable<t>

so should do:

public static class helper {   public static iqueryable<table> searchtext(     iqueryable<table> q,     string searchstring   )   {     var searchtype = -1;         if(searchstring.indexof("*") == -1)     {         searchtype = 0; // no wildcard     }     else if(searchstring.indexof("*") == 0 &&       searchstring.lastindexof("*") == searchstring.length - 1)     {         searchtype = 1; // start , end     }     else if(searchstring.indexof("*") == 0)     {         searchtype = 2; // ends     }     else if(searchstring.lastindexof("*") == searchstring.length - 1)     {         searchtype = 3; // starts     }      var search = searchstring.replace("*", "");      switch(searchtype)     {       default:       case 0: return q.where( o => o == search );       case 1: return q.where( o => o.text.contains( search ) );       case 2: return q.where( o => o.text.endswith( search ) );       case 3: return q.where( o => o.text.startswith( search ) );     }   } } 

where table.text property want search.

then can use this:

iqueryable<table> q = dbcontext.table;  var matches = q.searchtext( searchstring ).tolist(); 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -