Share via


データを並べ替える方法 (Entity Framework)

このトピックでは、クエリ結果を並べ替える方法について説明します。 この例は、Contact.LastName の先頭文字でアルファベット順に並べ替えられた Contact オブジェクトのコレクションを返します。 次の Entity Framework クエリ テクノロジを使用して同じことを行う例が紹介されています。

  • LINQ to Entities

  • Entity SQL と ObjectQuery<T>

  • ObjectQuery<T> のクエリ ビルダー メソッド

このトピックの例には、Adventure Works Sales Model が使用されています。このトピックのコードを実行するには、あらかじめプロジェクトに Adventure Works Sales Model を追加し、Entity Framework を使用するようにプロジェクトを構成しておく必要があります。詳細については、「Entity Data Model ウィザードを使用する方法 (Entity Framework)」、または「Entity Framework プロジェクトを手動で構成する方法」、および「Entity Data Model を手動で定義する方法 (Entity Framework)」を参照してください。

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

以下は、クエリ ビルダー メソッドの例です。

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

参照

概念

概念モデルに対するクエリ (Entity Framework)