Why does List<T> not implement IOrderedEnumerable<T>?

泄露秘密 提交于 2019-12-05 00:44:34
Jon Skeet

How could List<T> implement IOrderedEnumerable<T>? It would have to provide a way of creating a subsequent ordering... what does that even mean?

Consider this:

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

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 a List<T> - as you attempt to implement the CreateOrderedEnumerable method, I think you'll see why it's inappropriate. You may find my Edulinq blog post on IOrderedEnumerable<T> useful.

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, why List<T> doesn't implement IOrderedEnumerable<T>.
Just return the following:

ViewModel.SeriesRepository.OfType<T>().OrderBy(<your order predicate>);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!