クエリ式の構文例 : リレーションシップのナビゲーション (LINQ to Entities)

エンティティ データ モデル (EDM) のナビゲーション プロパティは、アソシエーションの末尾にあるエンティティを見つけるために使用できるショートカット プロパティです。ナビゲーション プロパティを使用すると、ユーザーは、エンティティ間をナビゲートしたり、あるエンティティからアソシエーション セットを介して関連エンティティにナビゲートしたりできます。このトピックでは、LINQ to Entities クエリ内でナビゲーション プロパティを介してリレーションシップをナビゲートするためのクエリ式の構文例を示します。

これらの例で使用されている、AdventureWorks Sales Model は、AdventureWorks サンプル データベースの Contact、Address、Product、SalesOrderHeader、SalesOrderDetail の各テーブルから作成されています。

このトピックの例には、次の using/Imports ステートメントが使用されています。

Option Explicit On
Option Strict On
Imports L2EExamplesVB.AdventureWorksModel
Imports System.Data.Objects
Imports System.Globalization
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using AdventureWorksModel;
using System.Globalization;

詳細については、「Visual Studio で LINQ to Entities プロジェクトを作成する方法」を参照してください。

次の例では、Select メソッドを使用して、姓が "Zhou" である連絡先のすべての連絡先 ID と、各連絡先の合計支払額の総計を取得します。Contact.SalesOrderHeader ナビゲーション プロパティは、各連絡先の SalesOrderHeader オブジェクトのコレクションを取得するために使用されます。Sum メソッドは、Contact.SalesOrderHeader ナビゲーション プロパティを使用して、それぞれの連絡先のすべての注文の合計支払額を合計します。

Using AWEntities As New AdventureWorksEntities
    Dim contacts As ObjectQuery(Of Contact) = AWEntities.Contact

    Dim ordersQuery = From contact In contacts _
        Where contact.LastName = "Zhou" _
        Select New With _
                {.ContactID = contact.ContactID, _
                .Total = contact.SalesOrderHeader.Sum(Function(o) o.TotalDue)}

    For Each order In ordersQuery
        Console.WriteLine("Contact ID: {0} Orders total: {1}", order.ContactID, order.Total)
    Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
    ObjectQuery<Contact> contacts = AWEntities.Contact;

    var ordersQuery = from contact in contacts
                      where contact.LastName == "Zhou"
                      select new
                      {
                          ContactID = contact.ContactID,
                          Total = contact.SalesOrderHeader.Sum(o => o.TotalDue)
                      };

    foreach (var contact in ordersQuery)
    {
        Console.WriteLine("Contact ID: {0} Orders total: {1}", contact.ContactID, contact.Total);
    }
}

次の例では、姓が "Zhou" である連絡先のすべての注文を取得します。Contact.SalesOrderHeader ナビゲーション プロパティは、各連絡先の SalesOrderHeader オブジェクトのコレクションを取得するために使用されます。連絡先の名前と注文が匿名型で返されます。

Using AWEntities As New AdventureWorksEntities
    Dim contacts As ObjectQuery(Of Contact) = AWEntities.Contact

    Dim ordersQuery = From contact In contacts _
        Where contact.LastName = "Zhou" _
        Select New With _
                {.LastName = contact.LastName, _
                 .Orders = contact.SalesOrderHeader}

    For Each order In ordersQuery
        Console.WriteLine("Name: {0}", order.LastName)
        For Each orderInfo In order.Orders

            Console.WriteLine("Order ID: {0}, Order date: {1}, Total Due: {2}", _
                    orderInfo.SalesOrderID, orderInfo.OrderDate, orderInfo.TotalDue)
        Next

        Console.WriteLine("")
    Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
    ObjectQuery<Contact> contacts = AWEntities.Contact;

    var ordersQuery = from contact in contacts
                      where contact.LastName == "Zhou"
                      select new { LastName = contact.LastName, Orders = contact.SalesOrderHeader };

    foreach (var order in ordersQuery)
    {
        Console.WriteLine("Name: {0}", order.LastName);
        foreach (SalesOrderHeader orderInfo in order.Orders)
        {
            Console.WriteLine("Order ID: {0}, Order date: {1}, Total Due: {2}",
                orderInfo.SalesOrderID, orderInfo.OrderDate, orderInfo.TotalDue);
        }
        Console.WriteLine("");
    }
}

次の例では、SalesOrderHeader.Address ナビゲーション プロパティと SalesOrderHeader.Contact ナビゲーション プロパティを使用して、互いに関連付けられている Address オブジェクトと Contact オブジェクトのコレクションを取得します。各注文の連絡先の姓、住所、販売注文番号、および Seattle 市に対する合計支払額が匿名型で返されます。

Using AWEntities As New AdventureWorksEntities

    Dim orders As ObjectQuery(Of SalesOrderHeader) = AWEntities.SalesOrderHeader

    Dim ordersQuery = From order In orders _
             Where order.Address.City = "Seattle" _
             Select New With { _
                            .ContactLastName = order.Contact.LastName, _
                            .ContactFirstName = order.Contact.FirstName, _
                            .StreetAddress = order.Address.AddressLine1, _
                            .OrderNumber = order.SalesOrderNumber, _
                            .TotalDue = order.TotalDue}

    For Each orderInfo In ordersQuery
        Console.WriteLine("Name: {0}, {1}", orderInfo.ContactLastName, orderInfo.ContactFirstName)
        Console.WriteLine("Street address: {0}", orderInfo.StreetAddress)
        Console.WriteLine("Order number: {0}", orderInfo.OrderNumber)
        Console.WriteLine("Total Due: {0}", orderInfo.TotalDue)
        Console.WriteLine("")
    Next

End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
    ObjectQuery<SalesOrderHeader> orders = AWEntities.SalesOrderHeader;

    var ordersQuery = from order in orders
                      where order.Address.City == "Seattle"
                      select new
                      {
                          ContactLastName = order.Contact.LastName,
                          ContactFirstName = order.Contact.FirstName,
                          StreetAddress = order.Address.AddressLine1,
                          OrderNumber = order.SalesOrderNumber,
                          TotalDue = order.TotalDue
                      };

    foreach (var orderInfo in ordersQuery)
    {
        Console.WriteLine("Name: {0}, {1}", orderInfo.ContactLastName, orderInfo.ContactFirstName);
        Console.WriteLine("Street address: {0}", orderInfo.StreetAddress);
        Console.WriteLine("Order number: {0}", orderInfo.OrderNumber);
        Console.WriteLine("Total Due: {0}", orderInfo.TotalDue);
        Console.WriteLine("");
    }
}

次の例では、Where メソッドを使用して、2003 年 12 月 1 日以降に受けた注文を検索します。次に、order.SalesOrderDetail ナビゲーション プロパティを使用して、各注文の詳細を取得します。

Using AWEntities As New AdventureWorksEntities
    Dim orders As ObjectQuery(Of SalesOrderHeader) = AWEntities.SalesOrderHeader

    Dim query = _
        From order In orders _
        Where order.OrderDate >= New DateTime(2003, 12, 1) _
        Select order

    Console.WriteLine("Orders that were made after December 1, 2003:")
    For Each order In query
        Console.WriteLine("OrderID {0} Order date: {1:d} ", _
                order.SalesOrderID, order.OrderDate)
        For Each orderDetail In order.SalesOrderDetail
            Console.WriteLine("  Product ID: {0} Unit Price {1}", _
                orderDetail.ProductID, orderDetail.UnitPrice)
        Next
    Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
    ObjectQuery<SalesOrderHeader> orders = AWEntities.SalesOrderHeader;

    IQueryable<SalesOrderHeader> query =
        from order in orders
        where order.OrderDate >= new DateTime(2003, 12, 1)
        select order;


    Console.WriteLine("Orders that were made after December 1, 2003:");
    foreach (SalesOrderHeader order in query)
    {
        Console.WriteLine("OrderID {0} Order date: {1:d} ",
            order.SalesOrderID, order.OrderDate);
        foreach (SalesOrderDetail orderDetail in order.SalesOrderDetail)
        {
            Console.WriteLine("  Product ID: {0} Unit Price {1}",
                orderDetail.ProductID, orderDetail.UnitPrice);
        }
    }
}

参照

概念

クエリ式の構文例 (LINQ to Entities)