Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq 如何转换IORDerenumerable<;T>;到页面列表<;T>;哪个使用OrderBy,然后使用by?_Linq_C# 4.0_Sql Order By_Pagedlist - Fatal编程技术网

Linq 如何转换IORDerenumerable<;T>;到页面列表<;T>;哪个使用OrderBy,然后使用by?

Linq 如何转换IORDerenumerable<;T>;到页面列表<;T>;哪个使用OrderBy,然后使用by?,linq,c#-4.0,sql-order-by,pagedlist,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 Feature

假设您有一个类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;}
}
在某个地方,您可以通过
页面列表加载所有产品:

排序的结果可以被视为
IEnumerable
IORDerenumerable

问题是当我们试图将排序后的
转换回
页面列表
,或
列表


有没有办法将
排序的
再次转换为
列表

您的第一个问题是
Product.ProductName
Product.id
ProductFeature
的实例,它没有实现
IComparable
,因此您的
OrderBy
ThenBy
在尝试时失败枚举已排序的
的结果。这显然是异常消息告诉您的


您的第二个问题是,当您说“将
排序的
转换回
页面列表
,或
列表
”时,您没有告诉我们“转换”是什么意思,我想不管你的意思是什么,都可以通过将
ProductName
ProductID
作为实现
IComparable

的实例来解决。你的第一个问题是
Product.ProductName
Product.ProductID
都是
ProductFeature
的实例,而这些实例并不是实现
i可比较
,因此当您尝试枚举排序后的
结果时,
OrderBy
ThenBy
会失败。这显然是异常消息告诉您的


您的第二个问题是,当您说“将
排序的
转换回
页面列表
,或
列表
”时,您没有告诉我们“转换”是什么意思,我怀疑,不管你的意思是什么,实际上都可以通过将
ProductName
ProductID
作为实现
IComparable

的实例来解决!这正是问题所在!我想我将按顺序使用例如:
x.ProductName.FeatureName
by@Junior梅:这是一个可以接受的解决方案。一个稍微好一点的替代方法可能是使
ProductFeature
实现
i可比较
,并让
CompareTo
返回
CompareTo
FeatureName
的相应值的结果。很好!这正是问题所在!我想我将按顺序使用例如:
x.ProductName.FeatureName
by@Junior梅:这是一个可以接受的解决方案。稍微好一点的替代方法可能是使
ProductFeature
实现
i可比较
,并让
CompareTo
返回
CompareTo
FeatureName
的相应值的结果。
var list = db.GetProductList();//returns PagedList<Product>:
var sorted = model.Products.OrderBy(x => x.ProductName).ThenBy(x=>x.ProductID);
base {System.SystemException}: {"At least one object must implement IComparable."}
Message: "At least one object must implement IComparable."