Enumerable.ThenBy メソッド

定義

シーケンス内の要素の後続の順序を昇順で実行します。

オーバーロード

名前 説明
ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>)

指定した比較子を使用して、シーケンス内の要素の後続の順序を昇順で実行します。

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

キーに従って、シーケンス内の要素の後続の順序を昇順で実行します。

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

ソース:
OrderBy.cs
ソース:
OrderBy.cs
ソース:
OrderBy.cs
ソース:
OrderBy.cs
ソース:
OrderBy.cs

指定した比較子を使用して、シーケンス内の要素の後続の順序を昇順で実行します。

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)

型パラメーター

TSource

sourceの要素の型。

TKey

keySelectorによって返されるキーの型。

パラメーター

source
IOrderedEnumerable<TSource>

並べ替える要素を含む IOrderedEnumerable<TElement>

keySelector
Func<TSource,TKey>

各要素からキーを抽出する関数。

comparer
IComparer<TKey>

キーを比較する IComparer<T>

返品

キーに従って要素が並べ替えられる IOrderedEnumerable<TElement>

例外

source または keySelectornull

注釈

このメソッドは、遅延実行を使用して実装されます。 即時戻り値は、アクションの実行に必要なすべての情報を格納するオブジェクトです。 このメソッドで表されるクエリは、GetEnumerator メソッドを直接呼び出すか、C# で foreach を使用するか、Visual Basic で For Each を使用して列挙されるまで実行されません。

要素自体の値でシーケンスを並べ替える場合は、x => x の ID 関数 (C# では Function(x) x、Visual Basic では keySelector) を指定します。

ThenBy ThenByDescendingは、IOrderedEnumerable<TElement>型を拡張するために定義されています。これは、これらのメソッドの戻り値の型でもあります。 この設計では、任意の数の ThenBy または ThenByDescending メソッドを適用して、複数の並べ替え条件を指定できます。

Note

IOrderedEnumerable<TElement>IEnumerable<T>から継承されるため、OrderByOrderByDescendingOrderBy、またはOrderByDescendingの呼び出しの結果に対してThenByまたはThenByDescendingを呼び出すことができます。 これにより、以前に確立された順序を無視する新しいプライマリ順序が導入されます。

comparernullの場合は、キーの比較に既定の比較子Defaultが使用されます。

このメソッドは、安定した並べ替えを実行します。つまり、2 つの要素のキーが等しい場合、要素の順序は保持されます。 これに対し、不安定な並べ替えでは、同じキーを持つ要素の順序は保持されません。

適用対象

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

ソース:
OrderBy.cs
ソース:
OrderBy.cs
ソース:
OrderBy.cs
ソース:
OrderBy.cs
ソース:
OrderBy.cs

キーに従って、シーケンス内の要素の後続の順序を昇順で実行します。

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)

型パラメーター

TSource

sourceの要素の型。

TKey

keySelectorによって返されるキーの型。

パラメーター

source
IOrderedEnumerable<TSource>

並べ替える要素を含む IOrderedEnumerable<TElement>

keySelector
Func<TSource,TKey>

各要素からキーを抽出する関数。

返品

キーに従って要素が並べ替えられる IOrderedEnumerable<TElement>

例外

source または keySelectornull

次のコード例では、 ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>) を使用して、シーケンス内の要素のセカンダリ順序付けを実行する方法を示します。

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

注釈

このメソッドは、遅延実行を使用して実装されます。 即時戻り値は、アクションの実行に必要なすべての情報を格納するオブジェクトです。 このメソッドで表されるクエリは、GetEnumerator メソッドを直接呼び出すか、C# で foreach を使用するか、Visual Basic で For Each を使用して列挙されるまで実行されません。

要素自体の値でシーケンスを並べ替える場合は、x => x の ID 関数 (C# では Function(x) x、Visual Basic では keySelector) を指定します。

ThenBy ThenByDescendingは、IOrderedEnumerable<TElement>型を拡張するために定義されています。これは、これらのメソッドの戻り値の型でもあります。 この設計では、任意の数の ThenBy または ThenByDescending メソッドを適用して、複数の並べ替え条件を指定できます。

Note

IOrderedEnumerable<TElement>IEnumerable<T>から継承されるため、OrderByOrderByDescendingOrderBy、またはOrderByDescendingの呼び出しの結果に対してThenByまたはThenByDescendingを呼び出すことができます。 これにより、以前に確立された順序を無視する新しいプライマリ順序が導入されます。

このメソッドは、既定の比較子 Defaultを使用してキーを比較します。

このメソッドは、安定した並べ替えを実行します。つまり、2 つの要素のキーが等しい場合、要素の順序は保持されます。 これに対し、不安定な並べ替えでは、同じキーを持つ要素の順序は保持されません。

クエリ式の構文では、orderby [first criterion], [second criterion] (C#) または Order By [first criterion], [second criterion] (Visual Basic) 句は、ThenBy の呼び出しに変換されます。

こちらもご覧ください

適用対象