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>>)

根据某个键按升序对序列中的元素执行后续排序。

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<TSource>

一个 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> 一。 For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

该方法ThenBy<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>)生成一个表示调用ThenBy<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>)本身为构造泛型方法的一个MethodCallExpression方法。 然后,MethodCallExpressionCreateQuery<TElement>(Expression)它将传递给由参数属性source表示Provider的方法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>)

使用指定的比较器按升序对序列中的元素执行后续排序。

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<TSource>

一个 IOrderedQueryable<T>,将根据键对其元素排序。

例外

sourcekeySelectorcomparernull

注解

此方法至少有一个类型参数,其类型 Expression<TDelegate> 参数为类型之 Func<T,TResult> 一。 For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

该方法ThenBy<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>)生成一个表示调用ThenBy<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>)本身为构造泛型方法的一个MethodCallExpression方法。 然后,MethodCallExpressionCreateQuery<TElement>(Expression)它将传递给由参数属性source表示Provider的方法IQueryProvider。 调用 CreateQuery<TElement>(Expression) 的结果将强制转换为类型 IOrderedQueryable<T> 并返回。

执行表示调用 ThenBy<TSource,TKey>(IOrderedQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>) 的表达式树导致的查询行为取决于参数类型的 source 实现。 预期行为是,它执行基于通过调用keySelector每个元素source``source获取的键的元素的辅助类型。 保留以前建立的所有排序顺序。 该 comparer 参数用于比较键值。

适用于