다음을 통해 공유


방법: 관련 엔터티 로드(WCF Data Services)

WCF Data Services 에서 관련 엔터티를 로드해야 하는 경우 DataServiceContext 클래스의 LoadProperty 메서드를 사용할 수 있습니다. DataServiceQueryExpand 메서드를 사용하여 관련 엔터티가 같은 쿼리 응답에서 즉시 로드되도록 할 수도 있습니다.

이 항목의 예제에서는 Northwind 샘플 데이터 서비스 및 자동 생성된 클라이언트 데이터 서비스 클래스를 사용합니다. 이 서비스 및 클라이언트 데이터 클래스는 WCF Data Services 퀵 스타트를 완료하면 만들어집니다.

예제

다음 예제에서는 반환되는 각 Orders 인스턴스와 관련된 Customer를 명시적으로 로드하는 방법을 보여 줍니다.

' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)

Try
    ' Enumerate over the top 10 orders obtained from the context.
    For Each order As Order In context.Orders.Take(10)
        ' Explicitly load the customer for each order.
        context.LoadProperty(order, "Customer")

        ' Write out customer and order information.
        Console.WriteLine("Customer: {0} - Order ID: {1}", _
                order.Customer.CompanyName, order.OrderID)
    Next
Catch ex As DataServiceQueryException
    Throw New ApplicationException( _
            "An error occurred during query execution.", ex)
End Try
// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);

try
{
    // Enumerate over the top 10 orders obtained from the context.
    foreach (Order order in context.Orders.Take(10))
    {
        // Explicitly load the customer for each order.
        context.LoadProperty(order, "Customer");

        // Write out customer and order information.
        Console.WriteLine("Customer: {0} - Order ID: {1}", 
            order.Customer.CompanyName, order.OrderID);
    }
}
catch (DataServiceQueryException ex)
{
    throw new ApplicationException(
        "An error occurred during query execution.", ex);
}

다음 예제에서는 Expand 메서드를 사용하여 쿼리에서 반환된 Orders에 속하는 Order Details를 반환하는 방법을 보여 줍니다.

' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)

' Define a query for orders that also returns items and customers.
Dim query As DataServiceQuery(Of Order) = _
context.Orders.Expand("Order_Details,Customer")

Try
    ' Enumerate over the first 10 results of the query.
    For Each order As Order In query.Take(10)
        Console.WriteLine("Customer: {0}", order.Customer.CompanyName)
        Console.WriteLine("Order ID: {0}", order.OrderID)

        For Each item As Order_Detail In order.Order_Details
            Console.WriteLine("\tProduct: {0} - Quantity: {1}", _
                    item.ProductID, item.Quantity)
        Next
    Next
Catch ex As DataServiceQueryException
    Throw New ApplicationException( _
            "An error occurred during query execution.", ex)
End Try
// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);

// Define a query for orders that also returns items and customers.
DataServiceQuery<Order> query =
    context.Orders.Expand("Order_Details,Customer");

try
{
    // Enumerate over the first 10 results of the query.
    foreach (Order order in query.Take(10))
    {
        Console.WriteLine("Customer: {0}", order.Customer.CompanyName);
        Console.WriteLine("Order ID: {0}", order.OrderID);

        foreach (Order_Detail item in order.Order_Details)
        {
            Console.WriteLine("\tProduct: {0} - Quantity: {1}",
                item.ProductID, item.Quantity);
        }
    }
}
catch (DataServiceQueryException ex)
{
    throw new ApplicationException(
        "An error occurred during query execution.", ex);
}

참고 항목

개념

데이터 서비스 쿼리(WCF Data Services)