List 'Except' comparison - ignore case

john picture john · Sep 5, 2014 · Viewed 9.5k times · Source

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.

Answer

Damitha Shyamantha picture Damitha Shyamantha · Sep 5, 2014

Try this :)

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

Worked for me!