Share via


How to: Load Related Entities (ADO.NET Data Services)

When you need to load associated entities, you can use the LoadProperty(Object, String) method on the DataServiceContext class. You can also use the Expand(String) method on the DataServiceQuery to require that related entities be eagerly loaded in the same query response.

The example in this topic uses the Northwind sample data service and autogenerated client data service classes. This service and the client data classes are created when you complete the ADO.NET Data Services quickstart.

Example

The following example shows how to explicitly load the Customer that is related to each returned Orders instance.

' 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 Orders In context.Orders.Take(10)
        ' Explicitly load the customer for each order.
        context.LoadProperty(order, "Customers")

        ' Write out customer and order information.
        Console.WriteLine("Customer: {0} - Order ID: {1}", _
                order.Customers.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 (Orders order in context.Orders.Take(10))
    {
        // Explicitly load the customer for each order.
        context.LoadProperty(order, "Customers");

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

The following example shows how to use the Expand(String) method to return Order Details that belong to the Orders returned by the query.

' 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 Orders) = _
context.Orders.Expand("Order_Details,Customers")

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

        For Each item As Order_Details 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<Orders> query =
    context.Orders.Expand("Order_Details,Customers");

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

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

See Also

Other Resources

Querying the Data Service (ADO.NET Data Services)