如何将 IOrderedEnumerable转换为到 PagedList它使用 OrderBy 和 ThenBy?

发布于 2024-11-02 21:10:02 字数 1093 浏览 0 评论 0原文

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

var list = db.GetProductList();//returns PagedList<Product>:

现在您想要过滤并应用一些 < code>OrderBy 和 ThenBy

var sorted = model.Products.OrderBy(x => x.ProductName).ThenBy(x=>x.ProductID);

排序的结果可以视为 IEnumerable 并且也IOrderedEnumerable

问题是当我们尝试将 sorted 转换回 PagedListList 时。

base {System.SystemException}: {"At least one object must implement IComparable."}
Message: "At least one object must implement IComparable."

有什么方法可以将 sorted 再次转换为 List 吗?

suppose you have a class Product, with members that are declared as nested type 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;}
}

and somewhere you have a method to load all products as a PagedList<Product>:

var list = db.GetProductList();//returns PagedList<Product>:

now you want to filter and apply some OrderBy and ThenBy:

var sorted = model.Products.OrderBy(x => x.ProductName).ThenBy(x=>x.ProductID);

the result of sorted can be treated as IEnumerable<T> and also IOrderedEnumerable<T>.

the problem is when we try to convert sorted back to PagedList<Product>, or List<Product>.

base {System.SystemException}: {"At least one object must implement IComparable."}
Message: "At least one object must implement IComparable."

Any way to convert sorted as a List again?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只涨不跌 2024-11-09 21:10:02

您的第一个问题是 Product.ProductNameProduct.ProductIDProductFeature 的实例,它没有实现 IComparable 和因此,当您尝试枚举 sorted 的结果时,您的 OrderByThenBy 会失败。这显然是异常消息告诉您的内容。

您的第二个问题是,当您说“将 sorted 转换回 PagedListList时,您没有告诉我们“转换”的含义。 ;产品>。”但是,我怀疑您的意思实际上可以通过创建实现 IComparable 的实例来解决。

Your first problem is that Product.ProductName and Product.ProductID are instances of ProductFeature which does not implement IComparable and therefore your OrderBy and ThenBy fail when you try to enumerate over the results of sorted. This is clearly what the exception message is telling you.

Your second problem is that you didn't tell us what you mean by "convert" when you say "convert sorted back to PagedList<Product>, or List<Product>." However, I suspect whatever you mean will actually be resolved by making ProductName and ProductID instances of something that implement IComparable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文