Like query ing LINQ to Object

戏子无情 提交于 2019-12-06 11:34:29

问题


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.


回答1:


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.




回答2:


Also check

  StartsWith
   EndsWith

another alternate

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



回答3:


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



来源:https://stackoverflow.com/questions/2742667/like-query-ing-linq-to-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!