Home > Software engineering >  c# how to check if two Lists of SelectedListItem have the same values
c# how to check if two Lists of SelectedListItem have the same values

Time:11-27

I am struggling with the basic thing. I am trying to compare two lists of SelectedListItem. I would like to know if the two lists have the same values.

Let's imagine that we have

List<SelectedListItem> listA, listB

I already tried many ways like:

new HashSet<SelectListItem>( listA ).SetEquals( listB );

or

var firstNotSecond = listA .Except( listB ).ToList();
var secondNotFirst = listB .Except( listA ).ToList();

or

listA.Contains(listB[i])

or even simple

listA==listB

I am still getting the false output, but I am 100% sure that these values are the same since one list is created from the second one.

Is there a simple way to compare their items?

Thanks a lot

EDIT

Context: I created a listA from one database and put it into another empty database. Now I downloaded all the data from the second database as listB and I am trying to check if the data were changed

CodePudding user response:

You have to compare the values to compare the references will not work so maybe write a small helper method to check if two SelectedListItems have the same values and check if both lists have the same items.

If you want to do it the pretty way you could e.g. implement the IEqualityComparer<T> interface for the SelectedListItem class

If it is a quick and dirty single time check and the size of the tables isn't that big you could also serialize it and compare the output directly

  • Related