The following code could be used to search in an array / List of strings using LINQ.
String[] someArray
= { "Test1", "test2", "test3", "TEST4" };
string toCheck = "Test1";
if (someArray.Any(toCheck.Contains))
{
// found -> Do sth.
}
// or with list
List<string> someList
= new List<string>(new string[] { "TEST1", "test2", "test3", "TEST4" });
if (someList.Any(toCheck.Contains))
{
// "Test1" != "TEST1"
}
But how could you do this case invariant?
My approach was to convert the complete list to upper, and then test using contains:
if ((someList.ConvertAll(item => item.ToUpper()).Any(toCheck.ToUpper().Contains)))
{
// found -> Do sth.
}
In this case the original list is not altered.
if ((someList.Select(item => item.ToUpper()).Any(toCheck.ToUpper().Contains)))
{
// works with both
}
Well it works... (also with some language specific things like the turkish 'i' letter... (also we still don't have turkish customers as far as i know.. but who knows if they're are in future?)), but it seems not very elegant.
Is there a way to do an case invariant comparision if an item is in a list?
Best regards, Offler