I basically have a container that implements IEquatable (an example is shown below)
public class ContainerClass : IEquatable<ContainerClass> { public IEnumerable<CustomClass> CustomClass { get; set; } public override bool Equals(object obj) { ... } public bool Equals(ContainerClass other) { ... } public static bool operator ==(ContainerClass cc1, ContainerClass cc2) { ... } public static bool operator !=(ContainerClass cc1, ContainerClass cc2) { ... } public override int GetHashCode() { ... } }
and CustomClass, which also implements IEquatable
public class CustomClass : IEquatable<CustomClass> { public string stringone { get; set; } public string stringtwo { get; set; } public override bool Equals(object obj) { ... } public bool Equals(CustomClass other) { ... } public static bool operator ==(CustomClass cc1, CustomClass cc2) { ... } public static bool operator !=(CustomClass cc1, CustomClass cc2) { ... } public override int GetHashCode() { ... } }
All this works fine, for example, the following works
IEnumerable<CustomClass> customclassone = new List<CustomClass> { new CustomClass { stringone = "hi" }, new CustomClass { stringone = "lo" } }; IEnumerable<CustomClass> customclasstwo = new List<CustomClass> { new CustomClass { stringone = "hi" } }; var diff = customclassone.Except(customclasstwo); ContainerClass containerclassone = new ContainerClass { CustomClass = customclassone.AsEnumerable() }; ContainerClass containerclasstwo = new ContainerClass { CustomClass = customclasstwo.AsEnumerable() }; var diff2 = containerclassone.CustomClass.Except(customclasstwo.CustomClass);
After this code, both diff and diff2 contain the expected results when enumerated. However, if I then try
IEnumerable<CustomClass> oldCustom = oldContainerClass.CustomClass; IEnumerable<CustomClass> newcustom = newContainerClass.CustomClass; var exceptlist = oldCustom.Except(newcustom);
When I try to list the exceptions list, I get "At least one object must implement IComparable." The only difference between oldCustom and newCustom from those shown in the above working examples is how they are populated. Has anyone understood why this is happening?
source share