I am having a SeachBar
in Xamarin Forms, and ListView
that shows suggestions from existing list of cities. I want to get list of items matching search keyword irrespective of it being uppercase or lowercase.
For that I am having a List
. I want to Search items from that list and get List
of items that matched the search keyword ignoring the case. I am having a code for getting list of items that matched keyword. I just want it to include items that match search keyword ignoring the case of letters.
(Please note that here I want to return a List
of the matched items and not bool
whether match exists. So please do not close this question or mark similar to the one that returns bool
.)
Here is my code
List<string> allCities = new List<string> { "Mumbai", "Redmond", "Cambridge", "London", "Moscow", "New York", "Chicago"};
void SearchList()
{
string keyword = "mum";
var citiesSearched = allCities.Where(c => c.Contains(keyword));
ListView.ItemSource = citiesSearched;
}
I have tried with StringComparer.OrdinalIgnoreCase
and StringComparison.OrdinalIgnoreCase
but I was getting Error No overload for Contains takes 2 arguments.
This was the modification:
var citiesSearched = allCities.Where(c => c.Contains(keyword, StringComparer.OrdinalIgnoreCase));
// And
var citiesSearched = allCities.Where(c => c.Contains(keyword, StringComparison.OrdinalIgnoreCase));
in both the statements I got the error.