0

I'm creating a Search Bar and can't seem to get results back. It seems to be case sensitive. Is there any way I can make it case insensitive so the user can search in Lowercase or Uppercase and get the same results?

Here's my code thanks in advance!

  void SearchBar_TextChanged(object sender, TextChangedEventArgs e)
    {
        var _Container = BindingContext as PageViewModel;
        MyListView.BeginRefresh();

        if (String.IsNullOrWhiteSpace(e.NewTextValue))
            MyListView.ItemsSource = _Container.MyPageDetailCollection;
        else
            MyListView.ItemsSource = _Container.MyPageDetailCollection.Where(i => i.Name.Contains(e.NewTextValue));
        MyListView.EndRefresh();

    }
ItsZaccie
  • 3
  • 1
  • Option 1: `...Where(i => i.Name?.ToUpper().Contains(e.NewTextValue?.ToUpper()));`. Other options: [check this question](https://stackoverflow.com/questions/444798/case-insensitive-containsstring?answertab=scoredesc#tab-top) – Shaw Apr 29 '22 at 04:07

2 Answers2

0

We can first convert the input string into lower string.

You can refer to the following code:

string queryString = e.NewTextValue;

var normalizedQuery = queryString?.ToLower() ?? "";

MyListView.ItemsSource = _Container.MyPageDetailCollection.Where(f => f.Name.ToLowerInvariant().Contains(normalizedQuery)).ToList();
Jessie Zhang -MSFT
  • 9,830
  • 1
  • 7
  • 19
0

Consider converting strings to either lowercase/uppercase before doing the "where" LINQ query as follows:

void SearchBar_TextChanged(object sender, TextChangedEventArgs e)
{
    var _Container = BindingContext as PageViewModel;
    MyListView.BeginRefresh();

    if (String.IsNullOrWhiteSpace(e.NewTextValue))
        MyListView.ItemsSource = _Container.MyPageDetailCollection;
    else
        MyListView.ItemsSource = _Container.MyPageDetailCollection.Where(i => i.Name.ToLower().Contains(e.NewTextValue.ToLower()));
    MyListView.EndRefresh();

}
ponnu.m
  • 113
  • 6