Share via


如何:加载相关实体(WCF 数据服务)

如果需要在 WCF 数据服务 中加载关联实体,可以使用 DataServiceContext 类的 LoadProperty 方法。 还可以使用 DataServiceQuery<TElement>Expand 方法,以要求在同一查询响应中积极加载相关实体。

本主题中的示例使用 Northwind 示例数据服务和自动生成的客户端数据服务类。 此服务和这些客户端数据类是在完成 WCF 数据服务快速入门时创建的。

示例

下面的示例演示如何显式加载与每个返回的 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(vbTab & "Product: {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 数据服务)