How to: Sort Data

This topic shows how to sort query results. The example returns a collection of Contact objects sorted alphabetically by the first letter of Contact.LastName. The same example is shown using the following Entity Framework query technologies:

  • LINQ to Entities

  • Entity SQL with ObjectQuery<T>

  • Query builder methods of ObjectQuery<T>

The example in this topic is based on the Adventure Works Sales Model. To run the code in this topic, you must have already added the Adventure Works Sales Model to your project and configured your project to use the Entity Framework. For more information, see How to: Use the Entity Data Model Wizard (Entity Framework) or How to: Manually Configure an Entity Framework Project and How to: Manually Define an Entity Data Model (Entity Framework).

Example

The following is the LINQ to Entities example.

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

The following is the Entity SQL example.

' 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);
    }
}

The following is the query builder method example.

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

See Also

Concepts

Querying a Conceptual Model