class Program
{
static void Main(string[] args)
{
List<string> aList = new List<string>();
List<string> bList = new List<string>();
List<string> answerList = new List<string>();
aList.Add("and");
aList.Add("and");
aList.Add("AND");
aList.Add("And");
aList.Add("not");
aList.Add("noT");
bList.Add("NOt");
answerList = aList.Except(bList, StringComparer.InvariantCultureIgnoreCase).ToList();
foreach (var word in answerList)
{
Console.WriteLine(word);
}
}
The expected behavior of the above program is to remove all the occurrences of “not” in aList and return {and, and, AND, And}. It seems “StringComparer.InvariantCultureIgnoreCase” has removed all the duplicates of the word “and” and returned just one occurrence of {and} in answerList.
From the documentation of
Except()(emphasis mine):So,
Except()returns a set, which means it returns each string at most once. And since you’re telling it that case should be ignored, you’re getting the output you’re getting.To work around that, use a method that doesn’t operate on sets, like
Where():This approach is slow (O(a · b)) when compared with
Except()(O(a + b)), but that shouldn’t be a problem for short sequences.