假设您有一个Product类,其成员声明为嵌套类型ProductFeature:
public class Product {
public ProductFeature ProductID {get;set;}
public ProductFeature ProductName {get;set;}
}
public class ProductFeature {
public int FeatureID {get;set;}
public string FeatureName {get;set;}
}
并且您可以使用某种方法将所有产品加载为PagedList<Product>
:
var list = db.GetProductList();//returns PagedList<Product>:
现在您要过滤并应用一些OrderBy
和ThenBy
:
var sorted = model.Products.OrderBy(x => x.ProductName).ThenBy(x=>x.ProductID);
排序结果可以视为IEnumerable<T>
和IOrderedEnumerable<T>
。
问题是当我们尝试将sorted
转换回PagedList<Product>
或List<Product>
时。
base {System.SystemException}: {"At least one object must implement IComparable."}
Message: "At least one object must implement IComparable."
是否可以将sorted
再次转换为List
?
答案 0 :(得分:3)
您的首要问题是,Product.ProductName
和Product.ProductID
是ProductFeature
的实例,但未实施IComparable
因此OrderBy
和ThenBy
当您尝试枚举sorted
的结果时失败。这显然是异常消息告诉你的。
您的第二个问题是当您说“将sorted
转换回PagedList<Product>
或List<Product>
时,您没有告诉我们”转换“的含义。”但是,我怀疑通过制作实现ProductName
的{{1}}和ProductID
个实例来实际解决您的意思。