22

I have two lists and I want to compare them and get the differences, while ignoring any case differences.

I have used the following code to get the differences between the two lists but it does not ignore case differences.

IEnumerable<string> diff = list1.Except(list2);
List<string> differenceList = diff.ToList<string>();

I tried this:

IEnumerable<string> diff = list1.Except(list2, StringComparison.OrdinalIgnoreCase);

but Except does not seem to be having a string case check of that sort (so error). I'm hoping there's a work around.

john
  • 1,561
  • 3
  • 20
  • 44
  • 4
    That should probably be a `StringComparer`, not `StringComparison`. Pay attention to the types... – Jeff Mercado Sep 05 '14 at 06:27
  • See the [Except](http://msdn.microsoft.com/library/bb336390.aspx) overload with an [IEqualityComparer](http://msdn.microsoft.com/library/ms132151.aspx). As @JeffMercado said, there can already several `IEqualityComparer` be found under [StringComparer](http://msdn.microsoft.com/library/system.stringcomparer.aspx) – Corak Sep 05 '14 at 06:31
  • Thanks all :) I can't believe I didn't try `StringComparer`. – john Sep 05 '14 at 06:33
  • 2
    @SuperJohn - Then put it as an answer. :-) – Enigmativity Sep 05 '14 at 07:05

3 Answers3

48

Try this :)

List<string> except = list1.Except(list2, StringComparer.OrdinalIgnoreCase).ToList();

Worked for me!

JoeBilly
  • 3,057
  • 29
  • 35
Damitha Shyamantha
  • 514
  • 1
  • 4
  • 9
19

Here's what worked:

IEnumerable<string> differenceQuery = inputTable.Except(strArrList,
                                                        StringComparer.OrdinalIgnoreCase);

Used StringComparer instead of StringComparison.

john
  • 1,561
  • 3
  • 20
  • 44
-1

use StringComparer as shown below

Code:

list3 = list1.Except(list2,StringComparer.OrdinalIgnoreCase);
Vince
  • 945
  • 7
  • 17
Kundan
  • 22
  • 5