Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing 2 lists using Linq

Tags:

c#

linq

I have 2 lists I am trying to compare. I execute the following and I get a false value returned:

var areIdentical = list1.SequenceEqual(list2, myFileCompare);

That part is working. My lists are NOT equal. The problem is, I'm using the following command to try to find the differences:

var fileDiff = (from file in list1 
                select file).Except(list2, myFileCompare);

My problem is, fileDiff is returning an empty result set. Since I know they are NOT identical, shouldn't I get something returned? Perhaps my query is wrong on this. Any help would be appreciated! By the way, I can post more of my code, if you really need it, however, this should suffice.

like image 268
Icemanind Avatar asked Feb 23 '23 19:02

Icemanind


1 Answers

You wouldn't get anything if:

  • list2 contained everything in list1 but also extra items
  • the ordering was different

Assuming you don't care about the ordering, you can use:

var extraItemsInList2 = list2.Except(list1);
var extraItemsInList1 = list1.Except(list2);

If you do care about the order, you'll need to work out exactly how you want the differences to be represented.

like image 175
Jon Skeet Avatar answered Mar 06 '23 17:03

Jon Skeet