LINQ string contains another case-insensitive string

I am working in a C # Windows 8 Metro application and I am trying to filter out an ObservableCollection<T> using LINQ, where the property contains some string, and I need it to be case insensitive.

  var searchResults = from _rest in App.ViewModel.Restaurants where _rest.Name.IndexOf(queryText, StringComparison.CurrentCultureIgnoreCase) >= 0 select _rest; 

I work

  • Using string1.Contains(string2).ToUpper() on both lines.
  • Using string1.Contains(string2).ToLower() on both lines.
  • Using string1.IndexOf(string2, StringComparison.CurrentCultureIgnoreCase) >= 0 .
  • Using string1.IndexOf(string2, StringComparison.OrdinalIgnoreCase) >= 0 .
  • Using String.Compare(string1, string2, StringComparison.CurrentCultureIgnoreCase) .

But none of these methods works for me in a case-insensitive manner; it works fine if I spell the name correctly.

Does anyone have the same issue on Windows 8?

Thanks in advance for your help.

+6
source share
2 answers

Write your own extension method

 public static class MetroHelper { public static bool ContainsInvariant(this string mainText, string queryText) { return mainText.ToUpperInvariant().Contains(queryText.ToUpperInvariant()); } } 

and use in your application

 var searchResults = from _rest in App.ViewModel.Restaurants where _rest.Name.ContainsInvariant(queryText) select _rest; 

What I've done.

+1
source

try the following:

 var searchResults = from _rest in App.ViewModel.Restaurants where _rest.Name.IndexOf(queryText, StringComparison.InvariantCultureIgnoreCase) >= 0 select _rest; 
0
source

Source: https://habr.com/ru/post/918559/


All Articles