Gewusst wie: Sortieren von Daten (Entity Framework)

In diesem Thema wird gezeigt, wie Abfrageergebnisse sortiert werden. In diesem Beispiel wird eine Auflistung von Contact-Objekten zurückgegeben, die alphabetisch nach dem ersten Buchstaben von Contact.LastName sortiert sind. Dasselbe Beispiel wird unter Verwendung der folgenden Entity Framework -Abfragetechnologien gezeigt:

  • LINQ-to-Entities

  • Entity SQL mit ObjectQuery<T>

  • Abfrage-Generator-Methoden von ObjectQuery<T>

Das Beispiel in diesem Thema beruht auf dem Adventure Works Sales-Modell. Zum Ausführen des Codes in diesem Thema muss dem Projekt bereits das Adventure Works Sales-Modell hinzugefügt und das Projekt zur Verwendung von Entity Framework konfiguriert worden sein. Weitere Informationen finden Sie unter Gewusst wie: Verwenden des Assistenten für Entity Data Model (Entity Framework) bzw. Gewusst wie: Manuelles Konfigurieren eines Entity Framework-Projekts und Gewusst wie: Manuelles Definieren eines Entity Data Model (Entity Framework).

Beispiel

Im Folgenden handelt es sich um das LINQ to Entities -Beispiel.

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

Im Folgenden handelt es sich um das Entity SQL -Beispiel.

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

Im Folgenden wird ein Beispiel für die Abfrage-Generator-Methode dargestellt.

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

Siehe auch

Konzepte

Abfragen eines konzeptionellen Modells (Entity Framework)