Condividi tramite


Queryable.ThenByDescending Metodo

Definizione

Esegue un ordinamento successivo degli elementi in una sequenza in ordine decrescente.

Overload

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

Esegue un ordinamento successivo degli elementi in una sequenza in ordine decrescente, in base a una chiave.

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

Esegue un ordinamento successivo degli elementi in una sequenza in ordine decrescente utilizzando un operatore di confronto specificato.

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

Origine:
Queryable.cs
Origine:
Queryable.cs
Origine:
Queryable.cs
Origine:
Queryable.cs
Origine:
Queryable.cs

Esegue un ordinamento successivo degli elementi in una sequenza in ordine decrescente, in base a una chiave.

public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IOrderedQueryable<TSource> ^ ThenByDescending(System::Linq::IOrderedQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, TKey> ^> ^ keySelector);
public static System.Linq.IOrderedQueryable<TSource> ThenByDescending<TSource,TKey>(this System.Linq.IOrderedQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
public static System.Linq.IOrderedQueryable<TSource> ThenByDescending<TSource,TKey>(this System.Linq.IOrderedQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector);
static member ThenByDescending : System.Linq.IOrderedQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, 'Key>> -> System.Linq.IOrderedQueryable<'Source>
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
static member ThenByDescending : System.Linq.IOrderedQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, 'Key>> -> System.Linq.IOrderedQueryable<'Source>
<Extension()>
Public Function ThenByDescending(Of TSource, TKey) (source As IOrderedQueryable(Of TSource), keySelector As Expression(Of Func(Of TSource, TKey))) As IOrderedQueryable(Of TSource)

Parametri di tipo

TSource

Tipo degli elementi di source.

TKey

Tipo della chiave restituita dalla funzione rappresentata da keySelector.

Parametri

source
IOrderedQueryable<TSource>

Oggetto IOrderedQueryable<T> che contiene elementi da ordinare.

keySelector
Expression<Func<TSource,TKey>>

Funzione per estrarre una chiave da ogni elemento.

Restituisce

Oggetto IOrderedQueryable<T> i cui elementi sono ordinati in ordine decrescente in base a una chiave.

Attributi

Eccezioni

source o keySelector è null.

Commenti

Questo metodo ha almeno un parametro di tipo Expression<TDelegate> il cui argomento di tipo è uno dei Func<T,TResult> tipi . Per questi parametri, è possibile passare un'espressione lambda e verrà compilata in un oggetto Expression<TDelegate>.

Il ThenByDescending<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>) metodo genera un oggetto MethodCallExpression che rappresenta la chiamata ThenByDescending<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>) stessa come metodo generico costruito. Passa quindi l'oggetto MethodCallExpression al CreateQuery<TElement>(Expression) metodo dell'oggetto IQueryProvider rappresentato dalla Provider proprietà del source parametro . Il risultato della chiamata CreateQuery<TElement>(Expression) viene eseguito il cast al tipo IOrderedQueryable<T> e restituito.

Il comportamento della query che si verifica come risultato dell'esecuzione di un albero delle espressioni che rappresenta la chiamata ThenByDescending<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>) dipende dall'implementazione del tipo del source parametro. Il comportamento previsto è che esegue un tipo secondario degli elementi di source in ordine decrescente, in base alla chiave ottenuta richiamando keySelector su ogni elemento di source. Tutti gli ordini di ordinamento stabiliti in precedenza vengono mantenuti.

Si applica a

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

Origine:
Queryable.cs
Origine:
Queryable.cs
Origine:
Queryable.cs
Origine:
Queryable.cs
Origine:
Queryable.cs

Esegue un ordinamento successivo degli elementi in una sequenza in ordine decrescente utilizzando un operatore di confronto specificato.

public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IOrderedQueryable<TSource> ^ ThenByDescending(System::Linq::IOrderedQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, TKey> ^> ^ keySelector, System::Collections::Generic::IComparer<TKey> ^ comparer);
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);
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);
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")]
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);
static member ThenByDescending : System.Linq.IOrderedQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, 'Key>> * System.Collections.Generic.IComparer<'Key> -> System.Linq.IOrderedQueryable<'Source>
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("Enumerating collections as IQueryable can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")>]
static member ThenByDescending : System.Linq.IOrderedQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, 'Key>> * System.Collections.Generic.IComparer<'Key> -> System.Linq.IOrderedQueryable<'Source>
<Extension()>
Public Function ThenByDescending(Of TSource, TKey) (source As IOrderedQueryable(Of TSource), keySelector As Expression(Of Func(Of TSource, TKey)), comparer As IComparer(Of TKey)) As IOrderedQueryable(Of TSource)

Parametri di tipo

TSource

Tipo degli elementi di source.

TKey

Tipo della chiave restituita dalla keySelector funzione .

Parametri

source
IOrderedQueryable<TSource>

Oggetto IOrderedQueryable<T> che contiene elementi da ordinare.

keySelector
Expression<Func<TSource,TKey>>

Funzione per estrarre una chiave da ogni elemento.

comparer
IComparer<TKey>

Oggetto IComparer<T> da confrontare con le chiavi.

Restituisce

Raccolta i cui elementi sono ordinati in ordine decrescente in base a una chiave.

Attributi

Eccezioni

source o o keySelectorcomparer è null.

Esempio

Nell'esempio di codice seguente viene illustrato come usare ThenByDescending<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>) per eseguire un ordinamento secondario degli elementi in una sequenza in ordine decrescente usando un operatore di confronto personalizzato.

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
*/
Class CaseInsensitiveComparer
    Implements IComparer(Of String)

    Function Compare(ByVal x As String, ByVal y As String) As Integer _
        Implements IComparer(Of String).Compare

        ' Compare values and ignore case.
        Return String.Compare(x, y, True)
    End Function
End Class

Sub ThenByDescendingEx1()
    Dim fruits() As String = _
        {"apPLe", "baNanA", "apple", "APple", "orange", "BAnana", "ORANGE", "apPLE"}

    ' Sort the strings first ascending by their length and 
    ' then descending by using a custom case insensitive comparer.
    Dim query = fruits.AsQueryable() _
        .OrderBy(Function(fruit) fruit.Length) _
        .ThenByDescending(Function(fruit) fruit, New CaseInsensitiveComparer())

    Dim output As New System.Text.StringBuilder
    For Each fruit As String In query
        output.AppendLine(fruit)
    Next

    ' Display the results.
    MsgBox(output.ToString())
End Sub

' This code produces the following output:

' apPLe
' apple
' APple
' apPLE
' orange
' ORANGE
' baNanA
' BAnana

Commenti

Questo metodo ha almeno un parametro di tipo Expression<TDelegate> il cui argomento di tipo è uno dei Func<T,TResult> tipi . Per questi parametri, è possibile passare un'espressione lambda e verrà compilata in un oggetto Expression<TDelegate>.

Il ThenBy<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>) metodo genera un oggetto MethodCallExpression che rappresenta la chiamata ThenBy<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>) stessa come metodo generico costruito. Passa quindi l'oggetto MethodCallExpression al CreateQuery<TElement>(Expression) metodo dell'oggetto IQueryProvider rappresentato dalla Provider proprietà del source parametro . Il risultato della chiamata CreateQuery<TElement>(Expression) viene eseguito il cast al tipo IOrderedQueryable<T> e restituito.

Il comportamento della query che si verifica come risultato dell'esecuzione di un albero delle espressioni che rappresenta la chiamata ThenBy<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>) dipende dall'implementazione del tipo del source parametro. Il comportamento previsto è che esegue un tipo secondario degli elementi di source in ordine decrescente, in base alla chiave ottenuta richiamando keySelector su ogni elemento di source. Tutti gli ordini di ordinamento stabiliti in precedenza vengono mantenuti. Il comparer parametro viene usato per confrontare i valori di chiave.

Si applica a