Queryable.ThenBy 方法

定義

依遞增順序,執行序列中項目的後續排序作業。

多載

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

依據索引鍵,按遞增順序執行序列中項目的後續排序作業。

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

使用指定的比較子,依遞增順序執行序列中項目的後續排序作業。

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

來源:
Queryable.cs
來源:
Queryable.cs
來源:
Queryable.cs

依據索引鍵,按遞增順序執行序列中項目的後續排序作業。

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

類型參數

TSource

source 項目的類型。

TKey

keySelector 表示之函式所傳回索引鍵的型別。

參數

source
IOrderedQueryable<TSource>

包含要排序之項目的 IOrderedQueryable<T>

keySelector
Expression<Func<TSource,TKey>>

用來從各個項目擷取索引鍵的函式。

傳回

依據索引鍵排序其項目的 IOrderedQueryable<T>

例外狀況

sourcekeySelectornull

範例

下列程式碼範例示範如何使用 ThenBy<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>) 來執行序列中專案的次要順序。

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

// Sort the strings first by their length and then
// alphabetically by passing the identity selector function.
IEnumerable<string> query =
    fruits.AsQueryable()
    .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
*/
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 selector function.
Dim query = fruits.AsQueryable() _
    .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
MsgBox(output.ToString())

'This code produces the following output:

'apple
'grape
'mango
'banana
'orange
'blueberry
'raspberry
'passionfruit

備註

這個方法至少有一個類型 Expression<TDelegate> 參數,其類型引數為其中一個 Func<T,TResult> 型別。 針對這些參數,您可以傳入 Lambda 運算式,並將它編譯為 Expression<TDelegate>

方法 ThenBy<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>) 會產生 , MethodCallExpression 表示 ThenBy<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>) 呼叫本身為建構的泛型方法。 然後,它會將 傳遞 MethodCallExpressionCreateQuery<TElement>(Expression) 參數的 屬性所 Provider 表示的 source 方法 IQueryProvider 。 呼叫 CreateQuery<TElement>(Expression) 的結果會轉換成類型 IOrderedQueryable<T> 並傳回。

執行表示呼叫 ThenBy<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>) 的運算式樹狀結構所產生的查詢行為,取決於參數類型的 source 實作。 預期的行為是它會根據 在 的每個元素上叫 keySelector 用 所取得的索引鍵,執行 的次要 source 專案 source 。 所有先前建立的排序次序都會保留。

適用於

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

來源:
Queryable.cs
來源:
Queryable.cs
來源:
Queryable.cs

使用指定的比較子,依遞增順序執行序列中項目的後續排序作業。

public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IOrderedQueryable<TSource> ^ ThenBy(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> ThenBy<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> ThenBy<TSource,TKey> (this System.Linq.IOrderedQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector, System.Collections.Generic.IComparer<TKey>? comparer);
static member ThenBy : System.Linq.IOrderedQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, 'Key>> * System.Collections.Generic.IComparer<'Key> -> System.Linq.IOrderedQueryable<'Source>
<Extension()>
Public Function ThenBy(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)

類型參數

TSource

source 項目的類型。

TKey

keySelector 表示之函式所傳回索引鍵的型別。

參數

source
IOrderedQueryable<TSource>

包含要排序之項目的 IOrderedQueryable<T>

keySelector
Expression<Func<TSource,TKey>>

用來從各個項目擷取索引鍵的函式。

comparer
IComparer<TKey>

用來比較金鑰的 IComparer<T>

傳回

依據索引鍵排序其項目的 IOrderedQueryable<T>

例外狀況

sourcekeySelectorcomparernull

備註

這個方法至少有一個類型 Expression<TDelegate> 參數,其類型引數為其中一個 Func<T,TResult> 型別。 針對這些參數,您可以傳入 Lambda 運算式,並將它編譯為 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) 參數的 屬性所 Provider 表示的 source 方法 IQueryProvider 。 呼叫 CreateQuery<TElement>(Expression) 的結果會轉換成類型 IOrderedQueryable<T> 並傳回。

執行表示呼叫 ThenBy<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>) 的運算式樹狀結構所產生的查詢行為,取決於參數類型的 source 實作。 預期的行為是它會根據 在 的每個元素上叫 keySelector 用 所取得的索引鍵,執行 的次要 source 專案 source 。 所有先前建立的排序次序都會保留。 參數 comparer 是用來比較索引鍵值。

適用於