Share via


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>

Die Beispiele in diesem Thema basieren auf dem AdventureWorks Sales-Modell. Um den Code in diesem Beispiel auszuführen, müssen Sie Ihrem Projekt bereits das "AdventureWorks Sales"-Modell hinzugefügt und das Projekt für die Verwendung von Entity Framework konfiguriert haben. Verwenden Sie dazu die Verfahren in Gewusst wie: Manuelles Konfigurieren eines Entity Framework-Projekts und Gewusst wie: Manuelles Definieren eines Entity Data Model (Entity Framework). Sie können auch den Assistenten für Entity Data Model zum Definieren des "AdventureWorks Sales"-Modells verwenden. Weitere Informationen finden Sie unter Gewusst wie: Verwenden des Assistenten für Entity Data Model (Entity Framework).

Beispiel

Es folgt das LINQ-to-Entities-Beispiel:

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

Es folgt das Entity SQL-Beispiel:

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

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

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

Siehe auch

Weitere Ressourcen

Abfragen eines Entity Data Model (Entity Framework-Aufgaben)