如何将IOrderedEnumerable <t>转换为使用OrderBy和ThenBy的PagedList <t>?</t> </t>

时间:2011-04-22 13:18:48

标签: linq c#-4.0 sql-order-by pagedlist

假设您有一个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>:

现在您要过滤并应用一些OrderByThenBy

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

1 个答案:

答案 0 :(得分:3)

您的首要问题是,Product.ProductNameProduct.ProductIDProductFeature的实例,但未实施IComparable因此OrderByThenBy当您尝试枚举sorted的结果时失败。这显然是异常消息告诉你的。

您的第二个问题是当您说“将sorted转换回PagedList<Product>List<Product>时,您没有告诉我们”转换“的含义。”但是,我怀疑通过制作实现ProductName的{​​{1}}和ProductID个实例来实际解决您的意思。