I am trying to search data in case insensitive way. For example, if i have the text "Davé"(With accent) , that should be searchable by "Dave"(Without accent). That search works and results are retrieved when i work on Entity objects directly, but does not
work when i try to search on a List.
eg: List<myClass> lst = new List<myClass>();
lst = ..... //Fill this with data from DB.
var otherList = lst.Where(x => x.name.Contains("Dave")).ToList();
Now this otherList should contain the result "Davé". When i write the same where condition on an Entity object, it works, but does not work in the above case.
I don't the reason why it works with Entity object as I can see you complete code and DB. But the solution to the problem can be achieved using String.Compare method
When you do this on entity framework it converts your LINQ to SQL query and it is case insensitive. However when you do this on a List, which is a object resides in the memory, it uses LINQ to Object which is case sensitive in this case.
String.Compare does string comparison so you cannot use it for Contains (partial match). One possible way is to just use lower case such as:
var otherList = lst.Where(x => x.name.ToLower().Contains("dave")).ToList();
[It is appreciated to mark this as an answer if it helps you. It also encourages others to contribute.]
- Keep thinking of what's in the past only makes you away from moving forward.
Thanks so much for the answers. This is how i fixed it, so that it works generally for all kinds of accents.
......................
String normalizedString = s.Normalize(NormalizationForm.FormD);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < normalizedString.Length; i++)
{
Char c = normalizedString[i];
if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
stringBuilder.Append(c);
}
suryaphani
Member
3 Points
15 Posts
Case Insensitive search does not work on list<..>
Jun 29, 2012 11:16 PM|LINK
I am trying to search data in case insensitive way. For example, if i have the text "Davé"(With accent) , that should be searchable by "Dave"(Without accent). That search works and results are retrieved when i work on Entity objects directly, but does not work when i try to search on a List.
eg: List<myClass> lst = new List<myClass>();
lst = ..... //Fill this with data from DB.
var otherList = lst.Where(x => x.name.Contains("Dave")).ToList();
Now this otherList should contain the result "Davé". When i write the same where condition on an Entity object, it works, but does not work in the above case.
HatSoft
Member
80 Points
30 Posts
Re: Case Insensitive search does not work on list<..>
Jun 29, 2012 11:49 PM|LINK
I don't the reason why it works with Entity object as I can see you complete code and DB. But the solution to the problem can be achieved using String.Compare method
var otherList = lst.Where(x => String.Compare(x.name, "Dave", CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace) == 0).ToList();
For more information on this method please read at http://msdn.microsoft.com/en-us/library/cc190529.aspx
chsuan
Member
542 Points
77 Posts
Re: Case Insensitive search does not work on list<..>
Jun 30, 2012 03:55 AM|LINK
@siryaphani:
When you do this on entity framework it converts your LINQ to SQL query and it is case insensitive. However when you do this on a List, which is a object resides in the memory, it uses LINQ to Object which is case sensitive in this case.
String.Compare does string comparison so you cannot use it for Contains (partial match). One possible way is to just use lower case such as:
var otherList = lst.Where(x => x.name.ToLower().Contains("dave")).ToList();- Keep thinking of what's in the past only makes you away from moving forward.
suryaphani
Member
3 Points
15 Posts
Re: Case Insensitive search does not work on list<..>
Jul 02, 2012 05:15 PM|LINK
Thanks a lot for the reply. I tried this code, but still dave does not search for davé ( accented text ). Any more ideas?
Frank Jiang ...
All-Star
16006 Points
1728 Posts
Microsoft
Re: Case Insensitive search does not work on list<..>
Jul 03, 2012 09:20 AM|LINK
var otherList = lst.Where(x => x.Name.Replace('é','e').Contains("Dave")).ToList();
Or
var otherList = lst.Where(x => Regex.Replace(x.Name,@"[^\u0000-\u007F]","").Contains("Dav")).ToList();
Feedback to us
Develop and promote your apps in Windows Store
suryaphani
Member
3 Points
15 Posts
Re: Case Insensitive search does not work on list<..>
Jul 03, 2012 05:57 PM|LINK
Thanks so much for the answers. This is how i fixed it, so that it works generally for all kinds of accents.
......................
String normalizedString = s.Normalize(NormalizationForm.FormD);
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < normalizedString.Length; i++)
{
Char c = normalizedString[i];
if (CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark)
stringBuilder.Append(c);
}
...........................