Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does List<T> not implement IOrderedEnumerable<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
    }

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.

like image 683
Marcel Avatar asked Mar 25 '11 08:03

Marcel


2 Answers

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>] 1 useful.

like image 123
Jon Skeet Avatar answered Sep 22 '22 06:09

Jon Skeet


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>);
like image 34
Daniel Hilgarth Avatar answered Sep 22 '22 06:09

Daniel Hilgarth