Condividi tramite


How to: Sort Data (Entity Framework)

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 examples in this topic are based on the Adventure Works Sales Model. To run the code in this example, you must have already added the AdventureWorks Sales Model to your project and configured your project to use the Entity Framework. To do this, complete the procedures in How to: Manually Configure an Entity Framework Project and How to: Manually Define an Entity Data Model (Entity Framework). You can also use the Entity Data Model Wizard to define the AdventureWorks Sales Model. For more information, see How to: Use the Entity Data Model Wizard (Entity Framework).

Example

The following is the LINQ to Entities example.

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

The following is the Entity SQL example.

' Define the Entity SQL query string that returns 
' Contact objects sorted by last name.
Dim queryString = "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());
    }
}

The following is the query builder method example.

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

See Also

Other Resources

Querying an Entity Data Model (Entity Framework Tasks)