英語で読む

次の方法で共有


Queryable.ThenByDescending メソッド

定義

シーケンス内の後続の要素を降順で配置します。

オーバーロード

ThenByDescending<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>)

キーに従って、シーケンス内の後続の要素を降順で配置します。

ThenByDescending<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>)

指定された比較子を使用して、シーケンス内の後続の要素を降順で配置します。

ThenByDescending<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>)

ソース:
Queryable.cs
ソース:
Queryable.cs
ソース:
Queryable.cs

キーに従って、シーケンス内の後続の要素を降順で配置します。

C#
public static System.Linq.IOrderedQueryable<TSource> ThenByDescending<TSource,TKey> (this System.Linq.IOrderedQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector);

型パラメーター

TSource

source の要素の型。

TKey

keySelector で表された関数によって返されるキーの型。

パラメーター

source
IOrderedQueryable<TSource>

並べ替える要素を格納している IOrderedQueryable<T>

keySelector
Expression<Func<TSource,TKey>>

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

戻り値

要素がキーに従って降順に並べ替えられている IOrderedQueryable<T>

例外

source または keySelectornull です。

注釈

このメソッドには、型引数が型 Expression<TDelegate> の 1 つである型のパラメーターが少なくとも 1 つ Func<T,TResult> 含まれています。 これらのパラメーターでは、ラムダ式を渡すと、 に Expression<TDelegate>コンパイルされます。

メソッドは ThenByDescending<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>)MethodCallExpression 構築されたジェネリック メソッドとしての呼び出し ThenByDescending<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>) 自体を表す を生成します。 次に、 パラメーターの MethodCallExpressionCreateQuery<TElement>(Expression) プロパティで表される の IQueryProvider メソッドに をProvidersource渡します。 を呼び出した CreateQuery<TElement>(Expression) 結果は 型 IOrderedQueryable<T> にキャストされ、返されます。

呼び出し ThenByDescending<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>) を表す式ツリーを実行した結果として発生するクエリ動作は、 パラメーターの型の source 実装によって異なります。 予期される動作は、 の各要素 source で を呼び出 keySelector すことによって取得されたキーに基づいて、 の要素 sourceのセカンダリ並べ替えを降順で実行することです。 以前に確立されたすべての並べ替え順序は保持されます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

ThenByDescending<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>)

ソース:
Queryable.cs
ソース:
Queryable.cs
ソース:
Queryable.cs

指定された比較子を使用して、シーケンス内の後続の要素を降順で配置します。

C#
public static System.Linq.IOrderedQueryable<TSource> ThenByDescending<TSource,TKey> (this System.Linq.IOrderedQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector, System.Collections.Generic.IComparer<TKey> comparer);
C#
public static System.Linq.IOrderedQueryable<TSource> ThenByDescending<TSource,TKey> (this System.Linq.IOrderedQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector, System.Collections.Generic.IComparer<TKey>? comparer);

型パラメーター

TSource

source の要素の型。

TKey

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

パラメーター

source
IOrderedQueryable<TSource>

並べ替える要素を格納している IOrderedQueryable<T>

keySelector
Expression<Func<TSource,TKey>>

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

comparer
IComparer<TKey>

キーを比較する IComparer<T>

戻り値

要素がキーに従って降順に並べ替えられているコレクション。

例外

sourcekeySelector、または comparer は、null です。

次のコード例では、 を使用して、カスタム 比較子を使用 ThenByDescending<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>) して、シーケンス内の要素のセカンダリ順序を降順で実行する方法を示します。

C#
public class CaseInsensitiveComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {
        return string.Compare(x, y, true);
    }
}

public static void ThenByDescendingEx1()
{
    string[] fruits =
    { "apPLe", "baNanA", "apple", "APple", "orange", "BAnana", "ORANGE", "apPLE" };

    // Sort the strings first ascending by their length and
    // then descending using a custom case insensitive comparer.
    IEnumerable<string> query =
        fruits.AsQueryable()
        .OrderBy(fruit => fruit.Length)
        .ThenByDescending(fruit => fruit, new CaseInsensitiveComparer());

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

/*
    This code produces the following output:

    apPLe
    apple
    APple
    apPLE
    orange
    ORANGE
    baNanA
    BAnana
*/

注釈

このメソッドには、型引数が型 Expression<TDelegate> の 1 つである型のパラメーターが少なくとも 1 つ Func<T,TResult> 含まれています。 これらのパラメーターでは、ラムダ式を渡すと、 に Expression<TDelegate>コンパイルされます。

メソッドは ThenBy<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>)MethodCallExpression 構築されたジェネリック メソッドとしての呼び出し ThenBy<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>) 自体を表す を生成します。 次に、 パラメーターの MethodCallExpressionCreateQuery<TElement>(Expression) プロパティで表される の IQueryProvider メソッドに をProvidersource渡します。 を呼び出した CreateQuery<TElement>(Expression) 結果は 型 IOrderedQueryable<T> にキャストされ、返されます。

呼び出し ThenBy<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>) を表す式ツリーを実行した結果として発生するクエリ動作は、 パラメーターの型の source 実装によって異なります。 予期される動作は、 の各要素 source で を呼び出 keySelector すことによって取得されたキーに基づいて、 の要素 sourceのセカンダリ並べ替えを降順で実行することです。 以前に確立されたすべての並べ替え順序は保持されます。 パラメーターは comparer 、キー値を比較するために使用されます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0