var listair = empcon.OrderBy(x => x.CustomerConnection.OrderBy(y => y.Id)).ToList();
When I am using this statement then I am getting exception "At least one object must implement IComparable"
How can I solve this problem?
It requires that implementing types define a single method, CompareTo(Object), that indicates whether the position of the current instance in the sort order is before, after, or the same as a second object of the same type. The instance's IComparable implementation is called automatically by methods such as Array.
C# IComparable interfaceThe IComparable interface defines a generalized type-specific comparison method that a value type or class implements to order or sort its instances. The IComparable is implemented by types whose values can be ordered or sorted. The interface requires the CompareTo method to be implemented.
The IComparer interface is used to sort elements that compare two objects and provides additional comparison method.
IComparer compares two objects that it's given. IComparable is implemented by the object that is being compared, for the purpose of comparing with another one of itself.
I had this problem with my query when I wrote it wrong:
IEnumerable<OrgRelation> relations = from r in tree.OrgRelations
orderby r.ParentUnit, r.ChildUnit
select r;
This was because the Parent and Child Units are both OrgUnit objects the are related to this OrgRelation entity. What I needed was to order not by the object, but by the property of the object on which I really wanted to sort. When I added the ".Name" it worked.
IEnumerable<OrgRelation> relations = from r in tree.OrgRelations
orderby r.ParentUnit.Name, r.ChildUnit.Name
select r;
Implement IComparable for the Types of the objects contained by CustomerConnection and empcon. If they don't have IComparable implemented then there is no way to perform an order by.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With