Share via


방법: 쿼리 경로를 사용하여 결과 셰이핑(Entity Framework)

이 항목에서는 개념적 모델에서 특정 개체를 쿼리할 때 반환되는 관련 개체를 정의하는 쿼리 경로를 지정하는 방법에 대한 예제를 제공합니다. 이 예제의 쿼리에서는 단일 Contact 개체 및 관련된 SalesOrderHeaderSalesOrderDetail 개체를 모두 반환합니다.

쿼리 경로를 지정하려면 문자열로 나타낸 개체 그래프를 ObjectQueryInclude 메서드에 전달합니다. Include 메서드는 모든 Entity Framework 쿼리(LINQ to Entities , Entity SQL 또는 쿼리 작성기 메서드)를 기반으로 적용될 수 있습니다.

이 항목의 예제는 Adventure Works Sales 모델을 기반으로 합니다. 이 항목의 코드를 실행하려면 프로젝트에 Adventure Works Sales 모델을 추가하고 프로젝트에서 Entity Framework를 사용하도록 구성해야 합니다. 자세한 내용은 방법: 엔터티 데이터 모델 마법사 사용(Entity Framework) 또는 방법: Entity Framework 프로젝트 수동 구성방법: 엔터티 데이터 모델 수동 정의(Entity Framework)를 참조하십시오.

예제

다음 예제에서는 Contact와 관련 SalesOrderHeaderSalesOrderDetail 개체를 반환하는 쿼리 경로를 지정합니다.

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 개요