2

i have a US states list

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

Now i have a string which contain some text like okl (it means Oklahoma for me). what i want i want 'like' query in List state and get Oklahoma state.

Pankaj
  • 4,419
  • 16
  • 50
  • 72

3 Answers3

4

Something like:

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

That's fine if the case matches as well, but it doesn't work so well for case-insensitive matches. For that, you might want something like:

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

Choose the exact string comparison you want appropriately - you might want to use the current culture, for example.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

Also check

  StartsWith
   EndsWith

another alternate

 var query = from c in ctx.Customers
                where SqlMethods.Like(c.City, "L_n%")
                select c;
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
0

If you want a really sophisticated approximate match check out Levenshtein distance in http://code.google.com/p/google-diff-match-patch/

Ian Mercer
  • 38,490
  • 8
  • 97
  • 133