Like LINQ to Object Query

I have a list of US states

List<string> state // contain all 51 US states

      

Now I have a string that contains some text like okl (to me this means Oklahoma). what i want, i want "like" query in "List" state and get oklahoma status.

+2


a source to share


3 answers


Sort of:

var matches = states.Where(state => state.Contains(searchText));

      

This is fine if the case also matches, but it doesn't work that well for case insensitive matches. To do this, you might need something like:



var matches = states.Where(state => 
      state.IndexOf(searchText, StringComparison.OrdinalIgnoreCase) != -1);

      

Choose the exact string comparison that you want to use correctly - for example, you can use the current culture.

+4


a source


Also check

  StartsWith
   EndsWith

      



another alternative

 var query = from c in ctx.Customers
                where SqlMethods.Like(c.City, "L_n%")
                select c;

      

+1


a source


If you want a really tricky approximate match check the Levenshtein distance at http://code.google.com/p/google-diff-match-patch/

0


a source







All Articles