为什么 List没有实现 IOrderedEnumerable

发布于 2024-10-25 19:00:59 字数 1599 浏览 1 评论 0原文

我想使用有序枚举,并使用接口作为返回类型而不是具体类型。我需要返回一组有序的对象。但是,当使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

度的依靠╰つ 2024-11-01 19:01:00

好吧,你错了:List 不是按特定键排序的。列表中的元素按照您放入的顺序排列。这就是 List 没有实现 IOrderedEnumerable 的原因。

只需返回以下内容:

ViewModel.SeriesRepository.OfType<T>().OrderBy(<your order predicate>);

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>);
顾铮苏瑾 2024-11-01 19:00:59

List如何实现IOrderedEnumerable?它必须提供一种创建后续排序的方法......这意味着什么?

考虑一下:

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

这到底意味着什么?没有主要排序顺序(如果我使用names.OrderBy(x => x)),因此不可能强加次要< /em> 排序顺序。

我建议您尝试基于 List 创建自己的 IOrderedEnumerable 实现 - 当您尝试实现 CreateOrderedEnumerable 方法,我想你会明白为什么它不合适。您可以找到我的 Edulinq 博客文章对 IOrderedEnumerable很有用。

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", "Ash", "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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文