次の方法で共有


ナビゲーション プロパティ

エンティティ データ モデル のナビゲーション プロパティは、アソシエーションの末尾にあるエンティティを見つけるために使用できるショートカット プロパティです。ナビゲーション プロパティを使用すると、エンティティ間をナビゲートしたり、あるエンティティからアソシエーション セットを介して関連エンティティにナビゲートしたりできます。ナビゲーション プロパティは、LINQ to Entities クエリの他に Entity SQL クエリでも使用できます。これらのプロパティを使用すると、JOIN 演算を実行しなくても、関連エンティティにアクセスできます。詳細については、「ナビゲーション プロパティ (EDM)」を参照してください。

次の例では、メソッドベースのクエリ構文で SelectMany メソッドを使用して、姓が "Zhou" である連絡先のすべての注文を取得します。Contact.SalesOrderHeader ナビゲーション プロパティは、各連絡先の SalesOrderHeader オブジェクトのコレクションを取得するために使用されます。

Using AWEntities As New AdventureWorksEntities
    Dim ordersQuery = AWEntities.Contact _
    .Where(Function(c) c.LastName = "Zhou") _
    .SelectMany(Function(o) o.SalesOrderHeader)

    For Each order In ordersQuery
        Console.WriteLine("Order ID: {0}, Order date: {1}, Total Due: {2}", _
                order.SalesOrderID, order.OrderDate, order.TotalDue)
    Next
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
    IQueryable<SalesOrderHeader> ordersQuery = AWEntities.Contact
        .Where(c => c.LastName == "Zhou")
        .SelectMany(c => c.SalesOrderHeader);

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

ナビゲーション プロパティは CLR のエンティティのインスタンスにもあり、ユーザーは Load メソッドを呼び出すことにより、関連エンティティを読み込むことができます。CLR でエンティティを読み込む場合は、エンティティのメンバにアクセスする前にナビゲーション プロパティを明示的に読み込む必要があります。これらのプロパティを読み込むことにより、データ ソースへの予期しないラウンド トリップを避けることができます。ただし、LINQ to Entities クエリはデータ ソースで評価されるので、追加のラウンド トリップは必要なく、プロパティはより複雑なナビゲーション式のプレースホルダの役割を果たします。

接続文字列で MultipleActiveResultSets=True が設定されている場合は、foreach/For Each ループ内のナビゲーション プロパティを明示的に読み込むことができます。接続文字列に MultipleActiveResultSets=False が含まれている場合、次の例の Load メソッド呼び出しによって、EntityCommandExecutionException がスローされます。

Using AWEntities As New AdventureWorksEntities()
    Dim contacts = _
        AWEntities.Contact _
        .Where(Function(c) c.LastName = "Johnson") _
        .Select(Function(c) c)

    Try
        For Each contact As Contact In contacts

            Console.WriteLine("Name: {0}, {1}", contact.LastName, contact.FirstName)

            ' Throws a EntityCommandExecutionException if 
            ' MultipleActiveResultSets is set to False in the 
            ' connection string.
            contact.SalesOrderHeader.Load()

            For Each order As SalesOrderHeader In contact.SalesOrderHeader
                Console.WriteLine("Order ID: {0}", order.SalesOrderID)
            Next

        Next
    Catch ex As EntityCommandExecutionException

        Console.WriteLine(ex.InnerException)
    End Try
End Using
using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
    IQueryable<Contact> contacts =
            AWEntities.Contact
            .Where(c => c.LastName == "Johnson")
            .Select(c => c);

    try
    {

        foreach (Contact contact in contacts)
        {
            Console.WriteLine("Name: {0}, {1}", contact.LastName, contact.FirstName);
            
            // Throws a EntityCommandExecutionException if 
            // MultipleActiveResultSets is set to False in the 
            // connection string.
            contact.SalesOrderHeader.Load();

            foreach (SalesOrderHeader order in contact.SalesOrderHeader)
            {
                Console.WriteLine("Order ID: {0}", order.SalesOrderID);
            }
        }
    }
    catch (EntityCommandExecutionException ex)
    {
        Console.WriteLine(ex.InnerException);
    }
}

参照

概念

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