public class ClassA
{
public enum1 Type { get;set;}
public List<ClassB> lst1 {get; set;}
}
public class ClassB
{
public string var1 { get;set;}
public int var2 { get;set;}
public List<int> var3 { get;set;}
public List<int> var4 { get;set;}
}
List<ClassA> lst1=Somefunction1();
List<ClassA> lst2=Somefunction2();
How can I check if lst1 and lst2 are equal considering order and also if I want to exclude one property var1
while comparing.
This is what I've got so far:
var firstNotSecond = lst1.Except(lst2).ToList();
var secondNotFirst = lst2.Except(lst1).ToList();
if ((firstNotSecond.Count > 0) || (secondNotFirst.Count > 0))
{
// not equal result = false;
}
As you can see, both lst1 and lst2 are lists of ClassA items. Normal definitions of lists being equal is that they are equal if each item in the first list equals the corresponding item in the second list. With corresponding I mean the item with the same index.
However, I don't want normal equality comparison of objects of class A, I want to specify specific equality comparers
- Two objects of ClassB are considered equal if the properties var2 / var3 / var 4 have equal value.
- Two objects of ClassA are equal if property Type has the same value and property lst1 of both classA objects are Enumerable.SequenceEqual
I assume this has to be done by implementing IEquality for these classes, or maybe creating an EqualityComparerer and Use SequenceEqual for my lst1 and lst2