Enumerable.ThenBy 方法

定义

按升序对序列中的元素执行后续排序。Performs a subsequent ordering of the elements in a sequence in ascending order.

重载

ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>)

使用指定的比较器按升序对序列中的元素执行后续排序。Performs a subsequent ordering of the elements in a sequence in ascending order by using a specified comparer.

ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>)

根据某个键按升序对序列中的元素执行后续排序。Performs a subsequent ordering of the elements in a sequence in ascending order according to a key.

ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>)

使用指定的比较器按升序对序列中的元素执行后续排序。Performs a subsequent ordering of the elements in a sequence in ascending order by using a specified comparer.

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);
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 的元素类型。The type of the elements of source.

TKey

keySelector 返回的键的类型。The type of the key returned by keySelector.

参数

source
IOrderedEnumerable<TSource>

一个包含要排序的元素的 IOrderedEnumerable<TElement>An IOrderedEnumerable<TElement> that contains elements to sort.

keySelector
Func<TSource,TKey>

用于从每个元素中提取键的函数。A function to extract a key from each element.

comparer
IComparer<TKey>

用于比较键的 IComparer<T>An IComparer<T> to compare keys.

返回

一个 IOrderedEnumerable<TElement>,将根据键对其元素排序。An IOrderedEnumerable<TElement> whose elements are sorted according to a key.

异常

sourcekeySelectornullsource or keySelector is null.

注解

此方法是使用延迟执行实现的。This method is implemented by using deferred execution. 即时返回值是一个对象,该对象存储执行操作所需的所有信息。The immediate return value is an object that stores all the information that is required to perform the action. 此方法表示的查询在枚举对象之前不会执行,方法是通过直接调用 GetEnumerator 方法或在 Visual Basic 中使用视觉C#对象中的 foreachFor Each 来枚举对象。The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.

若要按元素本身的值对序列进行排序,请为 keySelector指定标识函数(在C#视觉对象中为x => x,在 Visual Basic 中指定 Function(x) x)。To order a sequence by the values of the elements themselves, specify the identity function (x => x in Visual C# or Function(x) x in Visual Basic) for keySelector.

定义 ThenByThenByDescending 以扩展类型 IOrderedEnumerable<TElement>,这也是这些方法的返回类型。ThenBy and ThenByDescending are defined to extend the type IOrderedEnumerable<TElement>, which is also the return type of these methods. 此设计使你可以通过应用任意数量的 ThenByThenByDescending 方法来指定多个排序条件。This design enables you to specify multiple sort criteria by applying any number of ThenBy or ThenByDescending methods.

备注

由于 IOrderedEnumerable<TElement>IEnumerable<T>继承,因此,您可以对调用 OrderByOrderByDescendingThenByThenByDescending的结果调用 OrderByOrderByDescendingBecause IOrderedEnumerable<TElement> inherits from IEnumerable<T>, you can call OrderBy or OrderByDescending on the results of a call to OrderBy, OrderByDescending, ThenBy or ThenByDescending. 这样做会引入一个新的主要顺序,该顺序将忽略以前建立的排序。Doing this introduces a new primary ordering that ignores the previously established ordering.

如果 null``comparer,则使用默认比较器 Default 来比较键。If comparer is null, the default comparer Default is used to compare keys.

此方法执行稳定排序;也就是说,如果两个元素的键相等,则会保留元素的顺序。This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved. 与此相反,不稳定排序不会保留具有相同键的元素的顺序。In contrast, an unstable sort does not preserve the order of elements that have the same key.

ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>)

根据某个键按升序对序列中的元素执行后续排序。Performs a subsequent ordering of the elements in a sequence in ascending order according to a key.

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 的元素类型。The type of the elements of source.

TKey

keySelector 返回的键的类型。The type of the key returned by keySelector.

参数

source
IOrderedEnumerable<TSource>

一个包含要排序的元素的 IOrderedEnumerable<TElement>An IOrderedEnumerable<TElement> that contains elements to sort.

keySelector
Func<TSource,TKey>

用于从每个元素中提取键的函数。A function to extract a key from each element.

返回

一个 IOrderedEnumerable<TElement>,将根据键对其元素排序。An IOrderedEnumerable<TElement> whose elements are sorted according to a key.

异常

sourcekeySelectornullsource or keySelector is null.

示例

下面的代码示例演示如何使用 ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>) 对序列中的元素执行次要排序。The following code example demonstrates how to use ThenBy<TSource,TKey>(IOrderedEnumerable<TSource>, Func<TSource,TKey>) to perform a secondary ordering of the elements in a sequence.

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

注解

此方法是使用延迟执行实现的。This method is implemented by using deferred execution. 即时返回值是一个对象,该对象存储执行操作所需的所有信息。The immediate return value is an object that stores all the information that is required to perform the action. 此方法表示的查询在枚举对象之前不会执行,方法是通过直接调用 GetEnumerator 方法或在 Visual Basic 中使用视觉C#对象中的 foreachFor Each 来枚举对象。The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.

若要按元素本身的值对序列进行排序,请为 keySelector指定标识函数(在C#视觉对象中为x => x,在 Visual Basic 中指定 Function(x) x)。To order a sequence by the values of the elements themselves, specify the identity function (x => x in Visual C# or Function(x) x in Visual Basic) for keySelector.

定义 ThenByThenByDescending 以扩展类型 IOrderedEnumerable<TElement>,这也是这些方法的返回类型。ThenBy and ThenByDescending are defined to extend the type IOrderedEnumerable<TElement>, which is also the return type of these methods. 此设计使你可以通过应用任意数量的 ThenByThenByDescending 方法来指定多个排序条件。This design enables you to specify multiple sort criteria by applying any number of ThenBy or ThenByDescending methods.

备注

由于 IOrderedEnumerable<TElement>IEnumerable<T>继承,因此,您可以对调用 OrderByOrderByDescendingThenByThenByDescending的结果调用 OrderByOrderByDescendingBecause IOrderedEnumerable<TElement> inherits from IEnumerable<T>, you can call OrderBy or OrderByDescending on the results of a call to OrderBy, OrderByDescending, ThenBy or ThenByDescending. 这样做会引入一个新的主要顺序,该顺序将忽略以前建立的排序。Doing this introduces a new primary ordering that ignores the previously established ordering.

此方法使用默认比较器 Default来比较键。This method compares keys by using the default comparer Default.

此方法执行稳定排序;也就是说,如果两个元素的键相等,则会保留元素的顺序。This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved. 与此相反,不稳定排序不会保留具有相同键的元素的顺序。In contrast, an unstable sort does not preserve the order of elements that have the same key.

在查询表达式语法中,orderby [first criterion], [second criterion] (Visual C#)或 Order By [first criterion], [second criterion] (Visual Basic)子句转换为对 ThenBy的调用。In query expression syntax, an orderby [first criterion], [second criterion] (Visual C#) or Order By [first criterion], [second criterion] (Visual Basic) clause translates to an invocation of ThenBy.

另请参阅

适用于