AboutNothing
AboutNothing

Reputation: 107

SequenceEqual for specific properties

I would like to compare two lists of objects. The objects have four different properties. I need to compare only three of them as sequence (only three because one is always different).

An example would be as follows:

list1 = new List<myClass>() { new myClass(10, "a", 100, "unique1"),
    new myClass(10, "a", 100, "unique2") };

list2 = new List<myClass>() { new myClass(10, "a", 100, "unique3"),
    new myClass(10, "a", 100, "unique4") };

Can I compare those lists as sequence without the fourth (unique) property? Desired result for the exapmle would be TRUE.

Any ideas how to solve this?

Upvotes: 2

Views: 869

Answers (2)

BrokenGlass
BrokenGlass

Reputation: 161002

You can use the Enumerable.SequenceEqual overload that allows you to specify a custom IEqualityComparer - implement a custom one that only compares the three properties you are interested in.

Upvotes: 2

Reddog
Reddog

Reputation: 15579

You could use the SequenceEqual overload that allows you to pass in an IEqualityComparer<T> implementation that only includes the properties that you wish to compare.

The example on that linked page is a good one.

Upvotes: 3

Related Questions