如何:对数据进行排序(实体框架)

本主题介绍如何对查询结果进行排序。本示例返回 Contact 对象的集合,其结果按 Contact.LastName 首字母的字母顺序排序。下面使用以下 实体框架 查询技术演示同一示例:

  • LINQ to Entities

  • Entity SQL with ObjectQuery<T>

  • ObjectQuery <T> 的查询生成器方法

本主题中的示例基于 Adventure Works 销售模型。若要运行本示例中的代码,必须已将 AdventureWorks 销售模型添加到您的项目中,并将项目配置为使用实体框架。为此,请完成如何:手动配置实体框架项目如何:手动定义实体数据模型(实体框架) 中的过程。也可以使用实体数据模型向导定义 AdventureWorks 销售模型。有关更多信息,请参见如何:使用实体数据模型向导(实体框架)

示例

以下是 LINQ to Entities 示例。

Using context As AdventureWorksEntities = _
  New AdventureWorksEntities()
    Try
        ' Define a query that returns a list 
        ' of Contact objects sorted by last name.
        Dim sortedNames = _
        From n In context.Contact _
        Order By n.LastName _
        Select n

        Console.WriteLine("The sorted list of last names:")
        For Each name As Contact In sortedNames
            Console.WriteLine(name.LastName)
        Next
    Catch ex As EntitySqlException
        Console.WriteLine(ex.ToString())
    End Try
End Using
using (AdventureWorksEntities context = 
    new AdventureWorksEntities())
{
    try
    {
        // Define a query that returns a list 
        // of Contact objects sorted by last name.
        var sortedNames =
            from n in context.Contact
            orderby n.LastName
            select n;

        Console.WriteLine("The sorted list of last names:");
        foreach (Contact name in sortedNames)
        {
            Console.WriteLine(name.LastName);
        }
    }
    catch (EntitySqlException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

以下是 Entity SQL 示例。

' Define the Entity SQL query string that returns 
' Contact objects sorted by last name.
Dim queryString As String = "SELECT VALUE n FROM Contact AS n " _
        + "Order By n.LastName"

Using context As AdventureWorksEntities = _
    New AdventureWorksEntities()
    Try
        ' Define an ObjectQuery that returns a collection 
        ' of Contact objects sorted by last name.
        Dim query As ObjectQuery(Of Contact) = _
            New ObjectQuery(Of Contact)(queryString, context)

        Console.WriteLine("The sorted list of last names:")
        For Each name As Contact _
        In query.Execute(MergeOption.AppendOnly)
            Console.WriteLine(name.LastName)
        Next
    Catch ex As EntitySqlException
        Console.WriteLine(ex.ToString())
    End Try
End Using
// Define the Entity SQL query string that returns 
// Contact objects sorted by last name.
string queryString = @"SELECT VALUE n FROM Contact AS n 
        Order By n.LastName";

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    try
    {
        // Define an ObjectQuery that returns a collection 
        // of Contact objects sorted by last name.
        ObjectQuery<Contact> query = 
            new ObjectQuery<Contact>(queryString, context);

        Console.WriteLine("The sorted list of last names:");
        foreach (Contact name in query.Execute(MergeOption.AppendOnly))
        {
            Console.WriteLine(name.LastName);
        }
    }
    catch (EntitySqlException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

以下是查询生成器方法示例。

Using context As AdventureWorksEntities = _
    New AdventureWorksEntities()
    Try
        ' Define an ObjectQuery that returns a collection 
        ' of Contact objects sorted by last name.
        Dim query As ObjectQuery(Of Contact) = _
        context.Contact.OrderBy("it.LastName")

        Console.WriteLine("The sorted list of last names:")
        For Each name As Contact _
        In query.Execute(MergeOption.AppendOnly)
            Console.WriteLine(name.LastName)
        Next
    Catch ex As EntitySqlException
        Console.WriteLine(ex.ToString())
    End Try
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    try
    {
        // Define an ObjectQuery that returns a collection 
        // of Contact objects sorted by last name.
        ObjectQuery<Contact> query = 
            context.Contact.OrderBy("it.LastName");

        Console.WriteLine("The sorted list of last names:");
        foreach (Contact name in query.Execute(MergeOption.AppendOnly))
        {
            Console.WriteLine(name.LastName);
        }
    }
    catch (EntitySqlException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

另请参见

其他资源

查询实体数据模型(实体框架任务)