Condividi tramite


Procedura: ordinare i dati (Entity Framework)

In questo argomento viene descritto come ordinare i risultati di una query. L'esempio restituisce un insieme di oggetti Contact ordinati alfabeticamente in base alla prima lettera di Contact.LastName. Lo stesso esempio viene illustrato utilizzando le tecnologie di query Entity Framework seguenti:

  • LINQ to Entities

  • Entity SQL con ObjectQuery<T>

  • Metodi del generatore di query di ObjectQuery<T>

Gli esempi inclusi in questo argomento sono basati sul modello Sales di AdventureWorks. Per eseguire il codice incluso in questo esempio, è necessario avere già aggiunto il modello Sales di AdventureWorks al progetto e avere configurato il progetto per l'utilizzo di Entity Framework. A tale scopo, completare le procedure descritte in Procedura: configurare manualmente un progetto di Entity Framework e Procedura: definire manualmente un modello EDM (Entity Framework). È inoltre possibile utilizzare la procedura guidata Entity Data Model per definire il modello Sales di AdventureWorks. Per ulteriori informazioni, vedere Procedura: utilizzare la procedura guidata Entity Data Model (Entity Framework).

Esempio

Di seguito viene fornito un esempio di utilizzo di 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());
    }
}

Di seguito viene fornito un esempio di utilizzo di 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());
    }
}

Di seguito viene fornito un esempio del metodo del generatore di query.

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

Vedere anche

Altre risorse

Esecuzione di query su Entity Data Model (attività di Entity Framework)