HOW TO:排序資料 (Entity Framework)

本主題將說明如何排序查詢結果。 此範例會傳回依照 Contact.LastName 第一個字母的英文順序排序的 Contact 物件集合。 同時使用下列 Entity Framework 查詢技術顯示相同範例:

  • LINQ to Entities

  • Entity SQL with ObjectQuery<T>

  • Query builder methods of ObjectQuery<T>

本主題的範例根據 Adventure Works Sales Model。若要執行此主題中的程式碼,您必須已經將 Adventure Works Sales Model 加入到專案中,並設定您的專案使用 Entity Framework。如需詳細資訊,請參閱 HOW TO:使用實體資料模型精靈 (Entity Framework)HOW TO:手動設定 Entity Framework 專案HOW TO:手動設定 Entity Framework 專案

範例

以下是 LINQ to Entities 範例。

Using context As New AdventureWorksEntities()
    ' Define a query that returns a list 
    ' of Contact objects sorted by last name. 
    Dim sortedNames = From n In context.Contacts _
        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
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Define a query that returns a list 
    // of Contact objects sorted by last name.
    var sortedNames =
        from n in context.Contacts
        orderby n.LastName
        select n;

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

以下是 Entity SQL 範例。

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

Using context As New AdventureWorksEntities()
    ' Define an ObjectQuery that returns a collection 
    ' of Contact objects sorted by last name. 
    Dim query As 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
End Using
// Define the Entity SQL query string that returns 
// Contact objects sorted by last name.
string queryString = @"SELECT VALUE contact FROM Contacts AS contact 
        Order By contact.LastName";

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // 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);
    }
}

以下是查詢產生器方法範例。

Using context As New AdventureWorksEntities()
    ' Define an ObjectQuery that returns a collection 
    ' of Contact objects sorted by last name. 
    Dim query As ObjectQuery(Of Contact) = context.Contacts.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
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Define an ObjectQuery that returns a collection 
    // of Contact objects sorted by last name.
    ObjectQuery<Contact> query =
        context.Contacts.OrderBy("it.LastName");

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

另請參閱

概念

查詢概念模型 (Entity Framework)