Share via


方法: クエリ パスを使用して結果を構築する (Entity Framework)

このトピックでは、クエリ パスを指定する方法について説明します。クエリ パスは、概念モデルで特定のオブジェクトを照会したときに、関連するオブジェクトとして何が返されるかを定義するものです。 この例のクエリからは、単一の Contact オブジェクトのほかに、関連するすべての SalesOrderHeader オブジェクトおよび SalesOrderDetail オブジェクトが返されます。

クエリ パスを指定するには、オブジェクト グラフの文字列表記を ObjectQueryInclude メソッドに渡します。 Include メソッドは、任意の Entity Framework クエリ (LINQ to Entities 、Entity SQL 、またはクエリ ビルダー メソッド) の先頭で適用できます。

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

次の例では、Contact オブジェクト、および関連する SalesOrderHeader オブジェクトと SalesOrderDetail オブジェクトを返すクエリ パスを指定しています。

Using context As New AdventureWorksEntities()
    ' Define a LINQ query with a path that returns 
    ' orders and items for a contact. 
    Dim contacts = (From contact In context.Contacts.Include("SalesOrderHeaders.SalesOrderDetails") _
        Select contact).FirstOrDefault()

    ' Execute the query and display information for each item 
    ' in the orders that belong to the contact. 
    For Each order As SalesOrderHeader In contacts.SalesOrderHeaders
        Console.WriteLine(String.Format("PO Number: {0}", order.PurchaseOrderNumber))
        Console.WriteLine(String.Format("Order Date: {0}", order.OrderDate.ToString()))
        Console.WriteLine("Order items:")
        For Each item As SalesOrderDetail In order.SalesOrderDetails
            Console.WriteLine(String.Format("Product: {0} Quantity: {1}", _
                                              item.ProductID.ToString(), item.OrderQty.ToString()))
        Next
    Next
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Define a LINQ query with a path that returns 
    // orders and items for a contact.
    var contacts = (from contact in context.Contacts
                  .Include("SalesOrderHeaders.SalesOrderDetails")
                    select contact).FirstOrDefault();

    // Execute the query and display information for each item 
    // in the orders that belong to the contact.
    foreach (SalesOrderHeader order in contacts
        .SalesOrderHeaders)
    {
        Console.WriteLine(String.Format("PO Number: {0}",
            order.PurchaseOrderNumber));
        Console.WriteLine(String.Format("Order Date: {0}",
            order.OrderDate.ToString()));
        Console.WriteLine("Order items:");
        foreach (SalesOrderDetail item in order.SalesOrderDetails)
        {
            Console.WriteLine(String.Format("Product: {0} "
                + "Quantity: {1}", item.ProductID.ToString(),
                item.OrderQty.ToString()));
        }
    }
}

Include メソッドはクエリ オブジェクトを返します。このメソッドを ObjectQuery で複数回呼び出すことで、次の例のように複数のリレーションシップのエンティティを含めることができます。

' Create a SalesOrderHeader query with two query paths, 
' one that returns order items and a second that returns the 
' billing and shipping addresses for each order. 
Dim query As ObjectQuery(Of SalesOrderHeader) = context.SalesOrderHeaders.Include("SalesOrderDetails").Include("Address")
// Create a SalesOrderHeader query with two query paths, 
// one that returns order items and a second that returns the 
// billing and shipping addresses for each order.
ObjectQuery<SalesOrderHeader> query =
    context.SalesOrderHeaders.Include("SalesOrderDetails").Include("Address");

参照

処理手順

方法: エンティティ型オブジェクトを返すクエリを実行する (Entity Framework)
ナビゲーション プロパティを使用してリレーションシップをナビゲートする方法 (Entity Framework)

概念

関連オブジェクトの読み込み (Entity Framework)
クエリ ビルダー メソッド (Entity Framework)
LINQ to Entities
Entity SQL の概要