You can create your own extension method called "ContainsFuzzy":
public static bool ContainsFuzzy(this string target, string text){
Then your LINQ will be at least easier to read:
var objects = from x in db.Foo where x.Name.ContainsFuzzy("Foo McFoo") select x;
The obvious downside is that every call to ContainsFuzzy means re-creating your split list, etc., so there is some overhead. You can create a class called FuzzySearch, which at least will give you increased efficiency:
class FuzzySearch{ private string _searchTerm; private string[] _searchTerms; private Regex _searchPattern; public FuzzySearch( string searchTerm ){ _searchTerm = searchTerm; _searchTerms = searchTerm.Split( new Char[] { ' ' } ); _searchPattern = new Regex( "(?i)(?=.*" + String.Join(")(?=.*", _searchTerms) + ")"); } public bool IsMatch( string value ){
Your LINQ:
FuzzySearch _fuzz = new FuzzySearch( "Foo McFoo" ); var objects = from x in db.Foo where _fuzz.IsMatch( x.Name ) select x;
Jdb
source share