At least one object must implement IComparable

≡放荡痞女 提交于 2019-12-21 03:20:09

问题


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?


回答1:


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;



回答2:


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.



来源:https://stackoverflow.com/questions/6467272/at-least-one-object-must-implement-icomparable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!