Enumerable.ThenBy 方法

定义

按升序对序列中的元素执行后续排序。

重载

名称 说明
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>)

Source:
OrderBy.cs
Source:
OrderBy.cs
Source:
OrderBy.cs
Source:
OrderBy.cs
Source:
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>其元素根据键进行排序。

例外

sourcekeySelectornull.

示例

下面的代码示例演示如何用于 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 来枚举对象,否则不会执行此方法表示的查询。

若要按元素本身的值对序列进行排序,请在 C# 中指定标识函数(x => x 或 Visual Basic 中的 Function(x) x)作为 keySelector

ThenByThenByDescending 定义为扩展类型 IOrderedEnumerable<TElement>,这也是这些方法的返回类型。 通过此设计,可以通过应用任意数量的 ThenByThenByDescending 方法来指定多个排序条件。

注释

由于IOrderedEnumerable<TElement>继承自IEnumerable<T>,因此可以调用或OrderBy调用OrderByDescending的结果OrderByOrderByDescendingThenBy或者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>)

Source:
OrderBy.cs
Source:
OrderBy.cs
Source:
OrderBy.cs
Source:
OrderBy.cs
Source:
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>其元素根据键进行排序。

例外

sourcekeySelectornull.

注解

此方法是使用延迟执行实现的。 即时返回值是一个对象,用于存储执行操作所需的所有信息。 除非通过直接调用其 GetEnumerator 方法或在 C# 中使用 foreach 或在 Visual Basic 中使用 For Each 来枚举对象,否则不会执行此方法表示的查询。

若要按元素本身的值对序列进行排序,请在 C# 中指定标识函数(x => x 或 Visual Basic 中的 Function(x) x)作为 keySelector

ThenByThenByDescending 定义为扩展类型 IOrderedEnumerable<TElement>,这也是这些方法的返回类型。 通过此设计,可以通过应用任意数量的 ThenByThenByDescending 方法来指定多个排序条件。

注释

由于IOrderedEnumerable<TElement>继承自IEnumerable<T>,因此可以调用或OrderBy调用OrderByDescending的结果OrderByOrderByDescendingThenBy或者ThenByDescending。 这样做会引入一个新的主排序,忽略以前建立的排序。

comparer如果是null,则默认比较器Default用于比较键。

此方法执行稳定排序;也就是说,如果两个元素的键相等,则保留元素的顺序。 相反,不稳定的排序不会保留具有相同键的元素的顺序。

适用于