简体   繁体   中英

c# how to check if two Lists of SelectedListItem have the same values

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

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 eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM