Enumerable.ThenBy 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
執行後續的順序,按升序排列元素。
多載
| 名稱 | Description |
|---|---|
| ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>) |
根據索引鍵,依遞增順序執行序列中專案的後續順序。 |
| ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>) |
使用指定的比較子,依遞增順序執行序列中專案的後續順序。 |
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 或 keySelector 為 null。
範例
以下程式碼範例示範如何使用 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 的單位函數(C# 中的 Function(x) x 或 Visual Basic 中的 keySelector)。
ThenBy 和 ThenByDescending 定義為擴展型別 ,該型 IOrderedEnumerable<TElement>別同時也是這些方法的回傳型別。 此設計允許你透過應用任意數量ThenByThenByDescending的方法來指定多種排序條件。
Note
由於 IOrderedEnumerable<TElement> 繼承自 IEnumerable<T>,你可以在 、 或 OrderByOrderByDescending 的調用OrderByOrderByDescending結果上呼叫 ThenBy 或 ThenByDescending 。 這樣做會引入一個新的主序,忽略先前建立的排序。
此方法透過使用預設比較器 Default來比較金鑰。
此方法執行穩定排序;也就是說,如果兩個元素的鍵相等,則元素的順序會保持不變。 相較之下,不穩定排序則不會保留具有相同鍵的元素的順序。
在查詢表達式語法中,orderby [first criterion], [second criterion] (C#) 或 Order By [first criterion], [second criterion] (Visual Basic) 子句可轉換為 ThenBy 的呼叫。
另請參閱
適用於
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 或 keySelector 為 null。
備註
此方法透過延遲執行實現。 立即回傳值是一個物件,儲存執行動作所需的所有資訊。 此方法所代表的查詢,直到物件被枚舉,無論是直接呼叫其 GetEnumerator 方法,或是使用 C# 中的 foreach,或在 Visual Basic 中使用 For Each,才會執行。
若要依元素本身的值排序序列,請指定 x => x 的單位函數(C# 中的 Function(x) x 或 Visual Basic 中的 keySelector)。
ThenBy 和 ThenByDescending 定義為擴展型別 ,該型 IOrderedEnumerable<TElement>別同時也是這些方法的回傳型別。 此設計允許你透過應用任意數量ThenByThenByDescending的方法來指定多種排序條件。
Note
由於 IOrderedEnumerable<TElement> 繼承自 IEnumerable<T>,你可以在 、 或 OrderByOrderByDescending 的調用OrderByOrderByDescending結果上呼叫 ThenBy 或 ThenByDescending 。 這樣做會引入一個新的主序,忽略先前建立的排序。
若 comparer 是 null,則預設比較器 Default 用於比較金鑰。
此方法執行穩定排序;也就是說,如果兩個元素的鍵相等,則元素的順序會保持不變。 相較之下,不穩定排序則不會保留具有相同鍵的元素的順序。