Queryable.OrderBy 方法

定义

按升序对序列的元素进行排序。Sorts the elements of a sequence in ascending order.

重载

OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>)

使用指定的比较器按升序对序列的元素进行排序。Sorts the elements of a sequence in ascending order by using a specified comparer.

OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>)

根据键按升序对序列的元素进行排序。Sorts the elements of a sequence in ascending order according to a key.

OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>)

使用指定的比较器按升序对序列的元素进行排序。Sorts the elements of a sequence in ascending order by using a specified comparer.

public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IOrderedQueryable<TSource> ^ OrderBy(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, TKey> ^> ^ keySelector, System::Collections::Generic::IComparer<TKey> ^ comparer);
public static System.Linq.IOrderedQueryable<TSource> OrderBy<TSource,TKey> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector, System.Collections.Generic.IComparer<TKey> comparer);
public static System.Linq.IOrderedQueryable<TSource> OrderBy<TSource,TKey> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector, System.Collections.Generic.IComparer<TKey>? comparer);
static member OrderBy : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, 'Key>> * System.Collections.Generic.IComparer<'Key> -> System.Linq.IOrderedQueryable<'Source>
<Extension()>
Public Function OrderBy(Of TSource, TKey) (source As IQueryable(Of TSource), keySelector As Expression(Of Func(Of TSource, TKey)), comparer As IComparer(Of TKey)) As IOrderedQueryable(Of TSource)

类型参数

TSource

source 的元素类型。The type of the elements of source.

TKey

keySelector 表示的函数返回的键类型。The type of the key returned by the function that is represented by keySelector.

参数

source
IQueryable<TSource>

一个要排序的值序列。A sequence of values to order.

keySelector
Expression<Func<TSource,TKey>>

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

comparer
IComparer<TKey>

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

返回

IOrderedQueryable<TSource>

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

例外

sourcekeySelectorcomparernullsource or keySelector or comparer is null.

注解

此方法至少具有一个类型为的参数, Expression<TDelegate> 其类型参数为类型之一 Func<T,TResult>This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. 对于这些参数,可以传入 lambda 表达式,并将其编译为 Expression<TDelegate>For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>)方法生成一个 MethodCallExpression ,它表示调用 OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>) 自身作为构造的泛型方法。The OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>) method generates a MethodCallExpression that represents calling OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>) itself as a constructed generic method. 然后,它将传递 MethodCallExpressionCreateQuery<TElement>(Expression) IQueryProvider 由参数的属性表示的的方法 Provider sourceIt then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter. 调用的结果 CreateQuery<TElement>(Expression) 将强制转换为类型 IOrderedQueryable<T> ,并返回。The result of calling CreateQuery<TElement>(Expression) is cast to type IOrderedQueryable<T> and returned.

由于执行表示调用的表达式树而发生的查询行为 OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>) 取决于参数类型的实现 sourceThe query behavior that occurs as a result of executing an expression tree that represents calling OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>, IComparer<TKey>) depends on the implementation of the type of the source parameter. 预期的行为是根据 source 通过对 keySelector 的每个元素调用而获得的键对的元素进行排序 sourceThe expected behavior is that it sorts the elements of source based on the key obtained by invoking keySelector on each element of source. comparer参数用于比较键。The comparer parameter is used to compare keys.

OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>)

根据键按升序对序列的元素进行排序。Sorts the elements of a sequence in ascending order according to a key.

public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IOrderedQueryable<TSource> ^ OrderBy(System::Linq::IQueryable<TSource> ^ source, System::Linq::Expressions::Expression<Func<TSource, TKey> ^> ^ keySelector);
public static System.Linq.IOrderedQueryable<TSource> OrderBy<TSource,TKey> (this System.Linq.IQueryable<TSource> source, System.Linq.Expressions.Expression<Func<TSource,TKey>> keySelector);
static member OrderBy : System.Linq.IQueryable<'Source> * System.Linq.Expressions.Expression<Func<'Source, 'Key>> -> System.Linq.IOrderedQueryable<'Source>
<Extension()>
Public Function OrderBy(Of TSource, TKey) (source As IQueryable(Of TSource), keySelector As Expression(Of Func(Of TSource, TKey))) As IOrderedQueryable(Of TSource)

类型参数

TSource

source 的元素类型。The type of the elements of source.

TKey

keySelector 表示的函数返回的键类型。The type of the key returned by the function that is represented by keySelector.

参数

source
IQueryable<TSource>

一个要排序的值序列。A sequence of values to order.

keySelector
Expression<Func<TSource,TKey>>

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

返回

IOrderedQueryable<TSource>

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

例外

sourcekeySelectornullsource or keySelector is null.

示例

下面的代码示例演示如何使用 OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>) 对序列中的元素进行排序。The following code example demonstrates how to use OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>) to sort the elements of a sequence.

class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void OrderByEx1()
{
    Pet[] pets = { new Pet { Name="Barley", Age=8 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=1 } };

    // Sort the Pet objects in the array by Pet.Age.
    IEnumerable<Pet> query =
        pets.AsQueryable().OrderBy(pet => pet.Age);

    foreach (Pet pet in query)
        Console.WriteLine("{0} - {1}", pet.Name, pet.Age);
}

/*
    This code produces the following output:

    Whiskers - 1
    Boots - 4
    Barley - 8
*/

Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

Shared Sub OrderByEx1()
    Dim pets() As Pet = {New Pet With {.Name = "Barley", .Age = 8}, _
                         New Pet With {.Name = "Boots", .Age = 4}, _
                         New Pet With {.Name = "Whiskers", .Age = 1}}

    ' Sort the Pet objects in the array by Pet.Age.
    Dim query As IEnumerable(Of Pet) = _
                pets.AsQueryable().OrderBy(Function(pet) pet.Age)

    Dim output As New System.Text.StringBuilder
    For Each pet As Pet In query
        output.AppendLine(String.Format("{0} - {1}", pet.Name, pet.Age))
    Next

    ' Display the output.
    MsgBox(output.ToString())
End Sub

' This code produces the following output:

' Whiskers - 1
' Boots - 4
' Barley - 8

注解

此方法至少具有一个类型为的参数, Expression<TDelegate> 其类型参数为类型之一 Func<T,TResult>This method has at least one parameter of type Expression<TDelegate> whose type argument is one of the Func<T,TResult> types. 对于这些参数,可以传入 lambda 表达式,并将其编译为 Expression<TDelegate>For these parameters, you can pass in a lambda expression and it will be compiled to an Expression<TDelegate>.

OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>)方法生成一个 MethodCallExpression ,它表示调用 OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>) 自身作为构造的泛型方法。The OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>) method generates a MethodCallExpression that represents calling OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>) itself as a constructed generic method. 然后,它将传递 MethodCallExpressionCreateQuery<TElement>(Expression) IQueryProvider 由参数的属性表示的的方法 Provider sourceIt then passes the MethodCallExpression to the CreateQuery<TElement>(Expression) method of the IQueryProvider represented by the Provider property of the source parameter. 调用的结果 CreateQuery<TElement>(Expression) 将强制转换为类型 IOrderedQueryable<T> ,并返回。The result of calling CreateQuery<TElement>(Expression) is cast to type IOrderedQueryable<T> and returned.

由于执行表示调用的表达式树而发生的查询行为 OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>) 取决于参数类型的实现 sourceThe query behavior that occurs as a result of executing an expression tree that represents calling OrderBy<TSource,TKey>(IQueryable<TSource>, Expression<Func<TSource,TKey>>) depends on the implementation of the type of the source parameter. 预期的行为是根据 source 通过对 keySelector 的每个元素调用而获得的键对的元素进行排序 sourceThe expected behavior is that it sorts the elements of source based on the key obtained by invoking keySelector on each element of source.

适用于