0

Comparing 2 lists doesn't work as expected. I have one list 15800 count and the second with 15700 count. I have 3 similar items in both the Lists - MID, LID, CatalogID. There are some duplicates and some are not in the smaller list. All I need to do is find those that do not exist in the smaller List.

I am trying to find those records from the bigger list by using

List<Chemical> resultlist = essList.Except(demographicList).ToList();

When I use this, I see all the records from the bigger list in the resultlist.

Also, I need to compare all the fields from each of the list before giving me the result though.

Can someone tell me an easy way to get this done and help me learn what's wrong with except part.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
challengeAccepted
  • 7,106
  • 20
  • 74
  • 105
  • 1
    The default equality comparer, Default, is used to compare values of the types that implement the IEqualityComparer generic interface. To compare a custom data type, you need to implement this interface and provide your own GetHashCode and Equals methods for the type.https://msdn.microsoft.com/en-us/library/bb300779(v=vs.90).aspx – Marco Fatica Dec 17 '15 at 21:51

1 Answers1

0
List<Chemical> resultlist =
   essList.Where(e => !demographicList.Any(d =>
      d.MID == e.MID
        && d.LID == e.LID
        && d.CatalogID == e.CatalogID)).ToList();
Riad Baghbanli
  • 3,105
  • 1
  • 12
  • 20