At least one object must implement IComparable

中文:必须至少有一个对象实现 IComparable。

序列排序时报这个错误

lstReports.OrderBy(r => new { r.DepartmentName, r.ReportNo }).ToList(); //error occured

在LINQ to SQL/Entity中可以这么用,LINQ 2 Object 不能这么用,因为 new { r.DepartmentName, r.ReportNo } 这个匿名对象没有实现IComparable接口,也无法实现这个接口

其实多字段排序用 ThenBy/ThenByDescending 就可以:

lstReports.OrderBy(r => r.DepartmentName).ThenBy(r => r.ReportNo).ToList();

原文地址:https://www.cnblogs.com/felixnet/p/5193086.html