Enumerable.ThenBy 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
按升序对序列中的元素执行后续排序。
重载
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>)
根据某个键按升序对序列中的元素执行后续排序。
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<TSource>
一个 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
注解
此方法通过使用延迟执行来实现。 即时返回值是一个对象,用于存储执行操作所需的所有信息。 只有在通过直接调用其方法或在 Visual C# 中For Each
或Visual Basic中foreach
调用该GetEnumerator
对象,否则不会执行此方法表示的查询。
若要按元素本身的值对序列进行排序,请在 Visual C# 或 Function(x) x
Visual Basic) 中keySelector
指定标识函数 (x => x
。
ThenBy 并 ThenByDescending 定义为扩展类型 IOrderedEnumerable<TElement>,这也是这些方法的返回类型。 通过此设计,可以通过应用任意数量的 ThenBy 或 ThenByDescending 方法来指定多个排序条件。
备注
由于IOrderedEnumerable<TElement>继承自IEnumerable<T>,因此可以调用或OrderByDescending调用OrderBy结果 ThenBy OrderByOrderByDescending,或ThenByDescending调用结果。 执行此操作会引入一个新的主排序,忽略以前建立的排序。
此方法使用默认比较器 Default比较键。
此方法执行稳定排序;也就是说,如果两个元素的键相等,则保留元素的顺序。 相比之下,不稳定的排序不会保留具有相同键的元素的顺序。
在查询表达式语法中,orderby [first criterion], [second criterion]
(Visual C#) 或 Order By [first criterion], [second criterion]
(Visual Basic) 子句转换为调用 ThenBy。
另请参阅
适用于
ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>)
使用指定的比较器按升序对序列中的元素执行后续排序。
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<TSource>
一个 IOrderedEnumerable<TElement>,将根据键对其元素排序。
例外
source
或 keySelector
为 null
。
注解
此方法通过使用延迟执行来实现。 即时返回值是一个对象,用于存储执行操作所需的所有信息。 只有在通过直接调用其方法或在 Visual C# 中For Each
或Visual Basic中foreach
调用该GetEnumerator
对象,否则不会执行此方法表示的查询。
若要按元素本身的值对序列进行排序,请在 Visual C# 或 Function(x) x
Visual Basic) 中keySelector
指定标识函数 (x => x
。
ThenBy 并 ThenByDescending 定义为扩展类型 IOrderedEnumerable<TElement>,这也是这些方法的返回类型。 通过此设计,可以通过应用任意数量的 ThenBy 或 ThenByDescending 方法来指定多个排序条件。
备注
由于IOrderedEnumerable<TElement>继承自IEnumerable<T>,因此可以调用或OrderByDescending调用OrderBy结果 ThenBy OrderByOrderByDescending,或ThenByDescending调用结果。 执行此操作会引入一个新的主排序,忽略以前建立的排序。
null
如果是comparer
,则默认比较器Default用于比较键。
此方法执行稳定排序;也就是说,如果两个元素的键相等,则保留元素的顺序。 相比之下,不稳定的排序不会保留具有相同键的元素的顺序。