Share via


EntityCollection 内の関連するオブジェクトにクエリを実行する方法 (Entity Framework)

このトピックでは、リレーションシップ ナビゲーション プロパティによって返される EntityCollection 内の関連するオブジェクトを照会する方法について説明します。

The example in this topic is based on the Adventure Works Sales Model. この例のコードを実行するには、あらかじめプロジェクトに AdventureWorks Sales Model を追加し、Entity Framework が使用されるようにプロジェクトを構成しておく必要があります。 To do this, complete the procedures in Entity Framework プロジェクトを手動で構成する方法 and 方法: モデル ファイルとマッピング ファイルを手動で定義する (Entity Framework). Entity Data Model ウィザードを使用して、AdventureWorks Sales Model を定義することもできます。 詳細については、「Entity Data Model ウィザードを使用する方法 (Entity Framework)」を参照してください。

この例では、特定の連絡先に関連する SalesOrderHeader オブジェクトのコレクションを読み込んで、LINQ 式を使用して、既に出荷されたオンライン注文の一覧を返します。

' Specify the customer ID. 
Dim customerId As Integer = 4332

Using context As New AdventureWorksEntities()
    ' Get a specified customer by contact ID. 
    Dim customer = (From customers In context.Contacts _
        Where customers.ContactID = customerId _
        Select customers).First()

    ' You do not have to call the Load method to load the orders for the customer, 
    ' because lazy loading is set to true 
    ' by the constructor of the AdventureWorksEntities object. 
    ' With lazy loading set to true the related objects are loaded when 
    ' you access the navigation property. In this case SalesOrderHeaders. 

    ' Write the number of orders for the customer. 
    Console.WriteLine("Customer '{0}' has placed {1} total orders.", _
                      customer.LastName, customer.SalesOrderHeaders.Count)

    ' Get the online orders that have shipped. 
    Dim shippedOrders = From order In customer.SalesOrderHeaders _
        Where order.OnlineOrderFlag = True AndAlso order.Status = 5 _
        Select order

    ' Write the number of orders placed online. 
    Console.WriteLine("{0} orders placed online have been shipped.", shippedOrders.Count())
End Using
// Specify the customer ID.
int customerId = 4332;

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Get a specified customer by contact ID.
    var customer = (from customers in context.Contacts
                    where customers.ContactID == customerId
                    select customers).First();

    // You do not have to call the Load method to load the orders for the customer,
    // because  lazy loading is set to true 
    // by the constructor of the AdventureWorksEntities object. 
    // With  lazy loading set to true the related objects are loaded when
    // you access the navigation property. In this case SalesOrderHeaders.

    // Write the number of orders for the customer.
    Console.WriteLine("Customer '{0}' has placed {1} total orders.",
        customer.LastName, customer.SalesOrderHeaders.Count);

    // Get the online orders that have shipped.
    var shippedOrders =
        from order in customer.SalesOrderHeaders
        where order.OnlineOrderFlag == true
        && order.Status == 5
        select order;

    // Write the number of orders placed online.
    Console.WriteLine("{0} orders placed online have been shipped.",
        shippedOrders.Count());
}

この例では、SalesOrderHeader オブジェクトに対して最初の例と同じ LINQ クエリを使用します。 すべての関連するオブジェクトを最初にコレクションに読み込む代わりに、CreateSourceQuery メソッドが使用され、クエリによって返されたオブジェクトだけが読み込まれます。 次に、SalesOrderHeader リレーションシップ ナビゲーション プロパティによって返された EntityCollection に対して Load メソッドが呼び出され、残りの関連するオブジェクトが読み込まれます。

' Specify the customer ID. 
Dim customerId As Integer = 4332

Using context As New AdventureWorksEntities()
    ' Get a specified customer by contact ID. 
    Dim customer = (From customers In context.Contacts
        Where customers.ContactID = customerId
        Select customers).First()

    ' Use CreateSourceQuery to generate a query that returns 
    ' only the online orders that have shipped. 
    Dim shippedOrders = From orders In customer.SalesOrderHeaders.CreateSourceQuery() _
        Where orders.OnlineOrderFlag = True AndAlso orders.Status = 5 _
        Select orders

    ' Write the number of orders placed online. 
    Console.WriteLine("{0} orders placed online have been shipped.", shippedOrders.Count())

    ' You do not have to call the Load method to load the orders for the customer, 
    ' because lazy loading is set to true 
    ' by the constructor of the AdventureWorksEntities object. 
    ' With lazy loading set to true the related objects are loaded when 
    ' you access the navigation property. In this case SalesOrderHeaders. 

    ' Write the number of total orders for the customer. 
    Console.WriteLine("Customer '{0}' has placed {1} total orders.", _
                      customer.LastName, customer.SalesOrderHeaders.Count)
End Using
// Specify the customer ID.
int customerId = 4332;

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Get a specified customer by contact ID.
    var customer = (from customers in context.Contacts
                    where customers.ContactID == customerId
                    select customers).First();

    // Use CreateSourceQuery to generate a query that returns 
    // only the online orders that have shipped.
    var shippedOrders =
        from orders in customer.SalesOrderHeaders.CreateSourceQuery()
        where orders.OnlineOrderFlag == true
        && orders.Status == 5
        select orders;

    // Write the number of orders placed online.
    Console.WriteLine("{0} orders placed online have been shipped.",
        shippedOrders.Count());

    // You do not have to call the Load method to load the orders for the customer,
    // because  lazy loading is set to true 
    // by the constructor of the AdventureWorksEntities object. 
    // With  lazy loading set to true the related objects are loaded when
    // you access the navigation property. In this case SalesOrderHeaders.

    // Write the number of total orders for the customer.
    Console.WriteLine("Customer '{0}' has placed {1} total orders.",
        customer.LastName, customer.SalesOrderHeaders.Count);
}

参照

処理手順

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

概念

関連オブジェクトの読み込み (Entity Framework)