views:

42

answers:

3

Hello All

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.

+3  A: 

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  A: 

Also check

  StartsWith
   EndsWith

another alternate

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

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

Hightechrider