I had a list of strings and needed to check whether or not a specific string was contained within the list ignoring the character casing. Pre-LINQ days you would have had to loop through the entries calling the .ToLower or ToUpper method on each of the elements or using the string.Compare method. But thanks to LINQ, one simple method will take care of this for us:
List<string> list = new List<string>() { "a", "b", "c", "d", "e" }; string value = "A"; if (list.Contains(value, StringComparer.OrdinalIgnoreCase)) Console.WriteLine(value + " is in the list!"); else Console.WriteLine(value + " is not in the list!");
August 3, 2012 at 1:30 pm
Quick and to the point … thanks!
October 14, 2012 at 1:28 am
Tnanks very much – that is what I was looking for 🙂
July 10, 2013 at 3:33 am
Probably obvious to most but worth pointing out that you need the “using System.Linq;” directive to use this extension method.
October 1, 2014 at 5:14 am
This solution does not take care of space in your string
June 2, 2017 at 12:30 pm
Thanks buddy!
August 1, 2018 at 2:05 am
This is what was looking for. Many thanks