为什么列出< T>无法实现IOrderedEnumerable< T&gt ;? [英] Why does List<T> not implement IOrderedEnumerable<T>?

查看:164
本文介绍了为什么列出< T>无法实现IOrderedEnumerable< T&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用有序枚举,并使用接口作为返回类型,而不是具体类型.我需要返回有序对象集.但是,当使用IList<T>实现时,我不能返回IOrderedEnumerable<T>,因为IList<T>不继承IOrderedEnumerable<T> .

在下面的示例中,我有一个视图模型,其中包含系列的存储库,实现为系列对象的List<T>,因为它们位于List<T>中,因此是有序的.我是一个访问器方法,我想返回一系列过滤后的集合,其中仅返回特定类型的系列对象,同时保持过滤后的元素之间的原始顺序.

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
    }

编译器告诉我:

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的意图.

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.

推荐答案

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

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

考虑一下:

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

那甚至意味着什么?没有主要排序顺序(就像我使用names.OrderBy(x => x)一样),因此不可能强加次要排序顺序.

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.

我建议您尝试基于List<T>创建自己的IOrderedEnumerable<T>实现-当您尝试实现CreateOrderedEnumerable方法时,我想您会明白为什么它不合适.您可能会在以下位置找到我的 Edulinq博客文章IOrderedEnumerable<T> 有用.

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.

这篇关于为什么列出&lt; T&gt;无法实现IOrderedEnumerable&lt; T&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆