Linq. In addition to the function "At least one object must implement IComparable."

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?

+4
source share
1 answer

I suspect you tried to sort the contents of ContainerClass.CustomClass . Due to the delayed execution, you do not know that there is a problem until you go through it, and Except() is just a red herring. CustomClass does not implement the IComparable interface, so sorting is not performed with this error. Your CustomClass must either implement the IComparable<T> interface, or you must pass IComparer to IComparer OrderBy() .

eg.

 oldContainerClass.CustomClass = someListOfSomeType.OrderBy(x => x.CustomClasss, myComparer) .Select(x => x.CustomClass); 

Although this will help to understand what exactly you assigned to these properties, so that we can give you a more accurate reason.

+13
source

Source: https://habr.com/ru/post/1335537/


All Articles