19
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?

nvoigt
  • 75,013
  • 26
  • 93
  • 142
Amit
  • 191
  • 1
  • 1
  • 3
  • 1
    Well the code you posted isn't enought. What's empcon? What's CustomerConnection? You should provide more details. – Matteo Mosca Jun 24 '11 at 11:49

2 Answers2

26

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;
Joey Morgan
  • 271
  • 3
  • 7
  • yes because when using the Name this is a string and IComparable is already implemented on strings. – juFo Apr 12 '12 at 13:48
8

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.

Jeff Machamer
  • 942
  • 4
  • 7