Share via


方法: 関連エンティティを読み込む (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)