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.

Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
Rotten
  • 742
  • 10
  • 24
  • 5
    Can you give a short but complete example demonstrating the problem? – Jon Skeet Jun 20 '12 at 12:39
  • 1
    They aren't Continental European restaurant names are they? ;-) – dash Jun 20 '12 at 12:58
  • 1
    Does it work as you want it to if you apply the same LINQ expression in for example a .NET 4(.0) WPF application? – Anders Gustafsson Jun 20 '12 at 13:19
  • Made a simple .NET 4.5 Metro application (VS 2011 Beta, En-US culture) and applied your LINQ expression. Works as expected. Which culture are you using? – Anders Gustafsson Jun 20 '12 at 15:01
  • Can you give us some examples? – Lukasz Madon Jun 20 '12 at 18:22
  • Thanks for your responses. I'm working in ES-es Culture. – Rotten Jun 20 '12 at 20:02
  • What kind of example did you mean?? I'm loading the ObservableCollection deserializating an UTF-8 XML document, I have no issues here and the collection is populated fine. For example I have around 30 restaurants that contains "Despensa" in their names, if I search the word "Despensa", my app shows this 30 results, but if I search "despensa", "DESPENSA", or "DesPensA" the app shows me no results. I'll try to make a new empty Metro app this morning. – Rotten Jun 21 '12 at 06:42

2 Answers2

1

Write you 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;

That's what I did.

Imran Shaik
  • 305
  • 2
  • 6
0

try this:

var searchResults = from _rest in App.ViewModel.Restaurants
                         where  _rest.Name.IndexOf(queryText,
                                     StringComparison.InvariantCultureIgnoreCase) >= 0
                         select _rest;
Dean Chalk
  • 20,076
  • 6
  • 59
  • 90