Basically, I have a struct which contains a number of fields. I’m going to be creating two lists containing instances of this struct, and I need to be able to check to see if both lists are identical (i.e., same number of structs and have the same values for all of the fields).
I was looking into the SequenceEqual operator, but that relies on the items in the list having the same reference – they won’t, they’re both coming from different sources.
I could just order the lists by a field, loop through each item in the list, then loop through each field/property in the list and see if it matches in the other list – but it seems a bit overcomplicated. Is there an easier way?
The
SequenceEqualoperator compares using a comparer and not by reference of the sequences.From MSDN: “Determines whether two sequences are equal by comparing the elements by using the default equality comparer for their type.”
Use the
SequenceEqualand implement aIEqualityComparerand you should be fine.