为什么List 没有实现IOrderedEnumerable ?

我想使用有序的枚举,并使用接口作为返回类型而不是具体类型。 我需要返回一组有序的对象。 但是,当使用IList实现时, 我无法返回IOrderedEnumerable ,因为IList不会inheritanceIOrderedEnumerable

在下面的示例中,我有一个带有系列存储库的视图模型,实现为List的系列对象,它们位于List ,并且是有序的。 我是一个访问器方法,我想返回一个系列的过滤集,其中只返回特定类型的系列对象,同时保持过滤元素之间的原始顺序。

 ///  /// Represents the view model for this module. ///  public class ViewModel : AbstractViewModel { ///  /// Gets the series repository. ///  /// The series repository. public IList SeriesRepository { get; private set; } //... } //8<----------------------------- ///  /// Gets the series of the specified type. ///  public IOrderedEnumerable Series() where T : ISeries { return ViewModel.SeriesRepository.OfType(); //compiler ERROR } 

编译器告诉我:

 Error 14 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Linq.IOrderedEnumerable'. An explicit conversion exists (are you missing a cast?) ... 

我该如何支持这种情况? 为什么List没有实现IOrderedEnumerable?

编辑 :澄清我的意图:我只想在接口级别声明我的存储库有一个订单,即使它没有由一个键明确指定。 因此, .ThenBy等。 不应该添加新订单,因为已经存在一个 – 我自己的一个且只有一个。 :-)。 我知道,就像这样,我想念了.ThenBy

List如何实现IOrderedEnumerable ? 它必须提供一种创建后续订购的方式……这甚至意味着什么?

考虑一下:

 var names = new List { "Jon", "Holly", "Tom", "Robin", "William" }; var ordered = names.ThenBy(x => x.Length); 

那有什么意思? 没有主要的排序顺序(如果我使用names.OrderBy(x => x) ),所以不可能强加二级排序。

我建议您尝试基于List创建自己的IOrderedEnumerable实现 – 当您尝试实现CreateOrderedEnumerable方法时,我想您会明白为什么它不合适。 您可能会发现我在IOrderedEnumerable上的Edulinq博客文章很有用。

好吧,你错了: List 按特定键排序。 列表中的元素按照您放入的顺序排列。这就是为什么List没有实现IOrderedEnumerable
只需返回以下内容:

 ViewModel.SeriesRepository.OfType().OrderBy();