6

I'm working in a C# Windows 8 Metro app and I'm trying to filter an ObservableCollection<T> using LINQ where a property contains some string, and I need that it will be case insensitive.

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

I work around

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

But no one of this methods works for me in a case insensitive way, works ok if I write the name correctly.

Has someone have the same issue in Windows 8??

Thanks in advance for any help provided.

4

2 回答 2

1

编写你自己的扩展方法

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

并在您的应用程序中使用

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

这就是我所做的。

于 2012-10-17T15:19:23.053 回答
0

尝试这个:

var searchResults = from _rest in App.ViewModel.Restaurants
                         where  _rest.Name.IndexOf(queryText,
                                     StringComparison.InvariantCultureIgnoreCase) >= 0
                         select _rest;
于 2012-06-21T13:06:49.050 回答