Enumerable.ThenBy Metoda

Definicja

Wykonuje kolejność elementów w kolejności rosnącej.

Przeciążenia

ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>)

Wykonuje kolejność elementów w kolejności rosnącej zgodnie z kluczem.

ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>)

Wykonuje kolejność elementów w kolejności rosnącej przy użyciu określonego porównania.

ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>)

Źródło:
OrderBy.cs
Źródło:
OrderBy.cs
Źródło:
OrderBy.cs

Wykonuje kolejność elementów w kolejności rosnącej zgodnie z kluczem.

public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IOrderedEnumerable<TSource> ^ ThenBy(System::Linq::IOrderedEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector);
public static System.Linq.IOrderedEnumerable<TSource> ThenBy<TSource,TKey> (this System.Linq.IOrderedEnumerable<TSource> source, Func<TSource,TKey> keySelector);
static member ThenBy : System.Linq.IOrderedEnumerable<'Source> * Func<'Source, 'Key> -> System.Linq.IOrderedEnumerable<'Source>
<Extension()>
Public Function ThenBy(Of TSource, TKey) (source As IOrderedEnumerable(Of TSource), keySelector As Func(Of TSource, TKey)) As IOrderedEnumerable(Of TSource)

Parametry typu

TSource

Typ elementów elementu source.

TKey

Typ klucza zwróconego przez keySelector.

Parametry

source
IOrderedEnumerable<TSource>

Element IOrderedEnumerable<TElement> zawierający elementy do sortowania.

keySelector
Func<TSource,TKey>

Funkcja wyodrębniania klucza z każdego elementu.

Zwraca

Element IOrderedEnumerable<TElement> , którego elementy są sortowane zgodnie z kluczem.

Wyjątki

source lub keySelector to null.

Przykłady

W poniższym przykładzie kodu pokazano, jak wykonać ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>) kolejność pomocniczą elementów w sekwencji.

string[] fruits = { "grape", "passionfruit", "banana", "mango",
                      "orange", "raspberry", "apple", "blueberry" };

// Sort the strings first by their length and then
//alphabetically by passing the identity selector function.
IEnumerable<string> query =
    fruits.OrderBy(fruit => fruit.Length).ThenBy(fruit => fruit);

foreach (string fruit in query)
{
    Console.WriteLine(fruit);
}

/*
    This code produces the following output:

    apple
    grape
    mango
    banana
    orange
    blueberry
    raspberry
    passionfruit
*/
' Create an array of strings.
Dim fruits() As String =
{"grape", "passionfruit", "banana", "mango",
 "orange", "raspberry", "apple", "blueberry"}

' Sort the strings first by their length and then
' alphabetically by passing the identity function.
Dim query As IEnumerable(Of String) =
fruits _
.OrderBy(Function(fruit) fruit.Length) _
.ThenBy(Function(fruit) fruit)

' Display the results.
Dim output As New System.Text.StringBuilder
For Each fruit As String In query
    output.AppendLine(fruit)
Next
Console.WriteLine(output.ToString())

' This code produces the following output:
'
' apple
' grape
' mango
' banana
' orange
' blueberry
' raspberry
' passionfruit

Uwagi

Ta metoda jest implementowana za pomocą odroczonego wykonania. Bezpośrednio zwracana wartość jest obiektem, który przechowuje wszystkie informacje wymagane do wykonania akcji. Zapytanie reprezentowane przez tę metodę nie jest wykonywane, dopóki obiekt nie zostanie wyliczony, wywołując metodę GetEnumerator bezpośrednio lub używając w foreach języku C# lub For Each w Visual Basic.

Aby zamówić sekwencję według samych wartości elementów, określ funkcję identity (x => x w języku C# lub Function(x) x Visual Basic) dla elementu keySelector.

ThenBy i ThenByDescending są zdefiniowane w celu rozszerzenia typu IOrderedEnumerable<TElement>, który jest również zwracany typ tych metod. Ten projekt umożliwia określenie wielu kryteriów sortowania, stosując dowolną ThenBy liczbę metod lub ThenByDescending .

Uwaga

Ponieważ IOrderedEnumerable<TElement> dziedziczy z IEnumerable<T>klasy , można wywołać wywołanie OrderBy lub OrderByDescending na podstawie wyników wywołania metody OrderBy, OrderByDescendingThenBy lub ThenByDescending. Ten sposób wprowadza nowe podstawowe ustawianie kolejności, które ignoruje ustanowiony wcześniej sposób ustawiania.

Ta metoda porównuje klucze przy użyciu domyślnego modułu porównującego Default.

Ta metoda wykonuje stabilne sortowanie; oznacza to, że jeśli klawisze dwóch elementów są równe, kolejność elementów jest zachowywana. Z drugiej strony, niestabilne sortowanie nie pozwala zachować kolejności elementów, które mają ten sam klucz.

W składni wyrażenia zapytania klauzula orderby [first criterion], [second criterion] (C#) lub Order By [first criterion], [second criterion] (Visual Basic) tłumaczy się na wywołanie ThenBy.

Zobacz też

Dotyczy

ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>)

Źródło:
OrderBy.cs
Źródło:
OrderBy.cs
Źródło:
OrderBy.cs

Wykonuje kolejność elementów w kolejności rosnącej przy użyciu określonego porównania.

public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IOrderedEnumerable<TSource> ^ ThenBy(System::Linq::IOrderedEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, System::Collections::Generic::IComparer<TKey> ^ comparer);
public static System.Linq.IOrderedEnumerable<TSource> ThenBy<TSource,TKey> (this System.Linq.IOrderedEnumerable<TSource> source, Func<TSource,TKey> keySelector, System.Collections.Generic.IComparer<TKey> comparer);
public static System.Linq.IOrderedEnumerable<TSource> ThenBy<TSource,TKey> (this System.Linq.IOrderedEnumerable<TSource> source, Func<TSource,TKey> keySelector, System.Collections.Generic.IComparer<TKey>? comparer);
static member ThenBy : System.Linq.IOrderedEnumerable<'Source> * Func<'Source, 'Key> * System.Collections.Generic.IComparer<'Key> -> System.Linq.IOrderedEnumerable<'Source>
<Extension()>
Public Function ThenBy(Of TSource, TKey) (source As IOrderedEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), comparer As IComparer(Of TKey)) As IOrderedEnumerable(Of TSource)

Parametry typu

TSource

Typ elementów elementu source.

TKey

Typ klucza zwróconego przez keySelector.

Parametry

source
IOrderedEnumerable<TSource>

Element IOrderedEnumerable<TElement> zawierający elementy do sortowania.

keySelector
Func<TSource,TKey>

Funkcja wyodrębniania klucza z każdego elementu.

comparer
IComparer<TKey>

Element do IComparer<T> porównywania kluczy.

Zwraca

Element IOrderedEnumerable<TElement> , którego elementy są sortowane zgodnie z kluczem.

Wyjątki

source lub keySelector to null.

Uwagi

Ta metoda jest implementowana za pomocą odroczonego wykonania. Bezpośrednio zwracana wartość jest obiektem, który przechowuje wszystkie informacje wymagane do wykonania akcji. Zapytanie reprezentowane przez tę metodę nie jest wykonywane, dopóki obiekt nie zostanie wyliczony, wywołując metodę GetEnumerator bezpośrednio lub używając w foreach języku C# lub For Each w Visual Basic.

Aby zamówić sekwencję według samych wartości elementów, określ funkcję identity (x => x w języku C# lub Function(x) x Visual Basic) dla elementu keySelector.

ThenBy i ThenByDescending są zdefiniowane w celu rozszerzenia typu IOrderedEnumerable<TElement>, który jest również zwracany typ tych metod. Ten projekt umożliwia określenie wielu kryteriów sortowania, stosując dowolną ThenBy liczbę metod lub ThenByDescending .

Uwaga

Ponieważ IOrderedEnumerable<TElement> dziedziczy z IEnumerable<T>klasy , można wywołać wywołanie OrderBy lub OrderByDescending na podstawie wyników wywołania metody OrderBy, OrderByDescendingThenBy lub ThenByDescending. Ten sposób wprowadza nowe podstawowe ustawianie kolejności, które ignoruje ustanowiony wcześniej sposób ustawiania.

Jeśli comparer jest to null, domyślny moduł porównujący jest używany do porównywania Default kluczy.

Ta metoda wykonuje stabilne sortowanie; oznacza to, że jeśli klawisze dwóch elementów są równe, kolejność elementów jest zachowywana. Z drugiej strony, niestabilne sortowanie nie pozwala zachować kolejności elementów, które mają ten sam klucz.

Dotyczy