为什么 List没有实现 IOrderedEnumerable?
我想使用有序枚举,并使用接口作为返回类型而不是具体类型。我需要返回一组有序的对象。但是,当使用 IList
实现时,我无法返回 IOrderedEnumerable
,因为 IList
不会返回继承IOrderedEnumerable
。
在下面的示例中,我有一个带有系列存储库的视图模型,作为系列对象的List
实现,这些对象就像它们所在的那样在 List
中,已排序。在一个访问器方法中,我想返回一系列的过滤集,其中仅返回特定类型的系列对象,同时保持过滤元素之间的原始顺序。
/// <summary>
/// Represents the view model for this module.
/// </summary>
public class ViewModel : AbstractViewModel
{
/// <summary>
/// Gets the series repository.
/// </summary>
/// <value>The series repository.</value>
public IList<ISeries> SeriesRepository { get; private set; }
//...
}
//8<-----------------------------
/// <summary>
/// Gets the series of the specified type.
/// </summary>
public IOrderedEnumerable<T> Series<T>() where T : ISeries
{
return ViewModel.SeriesRepository.OfType<T>(); //compiler ERROR
}
编译器告诉我:
Error 14 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<T>' to 'System.Linq.IOrderedEnumerable<T>'. An explicit conversion exists (are you missing a cast?) ...
我怎样才能支持这样的场景?为什么 List 不实现 IOrderedEnumerable?
编辑:澄清我的意图:我只是想在接口级别声明我的存储库有一个顺序,即使没有明确指定通过钥匙。 因此,.ThenBy
等。不应添加新订单,因为已经有一个 - 我自己的一个,也是唯一一个。 :-)。我明白了,就像这样,我错过了 .ThenBy
的意图。
I would like to work with ordered enumerables, and use interfaces as return types rather than the concrete types. I need to return an ordered set of objects. But, when using an IList<T>
implementation I can not return IOrderedEnumerable<T>
, as IList<T>
does not inherit IOrderedEnumerable<T>
.
In the example below I have a view model with a repository of series, implemented as a List<T>
of series objects, which are, as they are residing in a List<T>
, ordered. I an accessor method, I want to return a filtered set of the series where only series objects of a specific type are returned, while keeping the original order among the filtered elements.
/// <summary>
/// Represents the view model for this module.
/// </summary>
public class ViewModel : AbstractViewModel
{
/// <summary>
/// Gets the series repository.
/// </summary>
/// <value>The series repository.</value>
public IList<ISeries> SeriesRepository { get; private set; }
//...
}
//8<-----------------------------
/// <summary>
/// Gets the series of the specified type.
/// </summary>
public IOrderedEnumerable<T> Series<T>() where T : ISeries
{
return ViewModel.SeriesRepository.OfType<T>(); //compiler ERROR
}
The compiler tells me:
Error 14 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<T>' to 'System.Linq.IOrderedEnumerable<T>'. An explicit conversion exists (are you missing a cast?) ...
How can I support such a scenario? And why does List not implement IOrderedEnumerable?
EDIT: To clarify my intentions: I simply want to declare at the interface level, that my Repository has an order, even if it is not explicitly specified by a key.
Thus, .ThenBy
et.al. should not add a new order, as there is already one - my own one and only one. :-). I see, that like so, I miss the intention of .ThenBy
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,你错了:
List
不是按特定键排序的。列表中的元素按照您放入的顺序排列。这就是List
没有实现IOrderedEnumerable
的原因。只需返回以下内容:
Well, you are wrong:
List<T>
is NOT ordered by a particular key. The elements inside the list are in the order you put them in. That's the reason, whyList<T>
doesn't implementIOrderedEnumerable<T>
.Just return the following:
List
如何实现IOrderedEnumerable
?它必须提供一种创建后续排序的方法......这意味着什么?考虑一下:
这到底意味着什么?没有主要排序顺序(如果我使用
names.OrderBy(x => x)
),因此不可能强加次要< /em> 排序顺序。我建议您尝试基于
List
创建自己的IOrderedEnumerable
实现 - 当您尝试实现CreateOrderedEnumerable
方法,我想你会明白为什么它不合适。您可以找到我的 Edulinq 博客文章对IOrderedEnumerable
很有用。How could
List<T>
implementIOrderedEnumerable<T>
? It would have to provide a way of creating a subsequent ordering... what does that even mean?Consider this:
what does that even mean? There's no primary sort order (as there would be if I used
names.OrderBy(x => x)
), so it's impossible to impose a secondary sort order.I suggest you try creating your own implementation of
IOrderedEnumerable<T>
based on aList<T>
- as you attempt to implement theCreateOrderedEnumerable
method, I think you'll see why it's inappropriate. You may find my Edulinq blog post onIOrderedEnumerable<T>
useful.