IOrderedEnumerable<T> Interface

Summary

Represents an ordered collection of elements. This interface is the base interface for LINQ query results that have been ordered.

Remarks

The IOrderedEnumerable<T> interface defines no members of its own. It inherits all members from IEnumerable<T>. However, it serves as a marker interface to indicate that the collection is ordered. This is useful for query operators that need to maintain ordering, such as ThenBy and ThenByDescending.

When you use LINQ extension methods like OrderBy or OrderByDescending, the results implement IOrderedEnumerable<T>. Subsequent ordering operations, like ThenBy, are applied to this ordered sequence.

Examples

Basic Usage

The following C# code example demonstrates how to use LINQ to order a collection of strings and then apply a secondary sort. The result of the primary sort is an IOrderedEnumerable<T>.

using System;
using System.Collections.Generic;
using System.Linq;

public class Example
{
    public static void Main()
    {
        List<string> words = new List<string> { "grape", "apple", "banana", "cherry", "apricot" };

        // Primary sort by length, secondary sort by alphabetical order
        IOrderedEnumerable<string> orderedWords = words
            .OrderBy(w => w.Length)
            .ThenBy(w => w);

        foreach (var word in orderedWords)
        {
            Console.WriteLine(word);
        }
        // Output:
        // grape
        // apple
        // cherry
        // banana
        // apricot
    }
}

Methods

This interface inherits methods from IEnumerable<T>. The primary purpose of IOrderedEnumerable<T> is to represent ordered sequences, and its behavior is primarily defined by the LINQ operators that produce it.

Method Description
IEnumerator<T>
GetEnumerator()
Returns an enumerator that iterates through the collection. Inherited from IEnumerable<T>.
IEnumerator
IEnumerable.GetEnumerator()
Returns an enumerator that iterates through a collection. Inherited from IEnumerable.

Interfaces

This interface implements the following interfaces:

Classes that implement IOrderedEnumerable<T>

The following classes internally implement or represent the concept of an ordered enumerable: