How to: Use Query Paths to Shape Results

This topic provides an example of how to specify a query path that defines which related objects are returned when a specific object is queried in a conceptual model. The query in this example returns a single Contact object and all of the related SalesOrderHeader and SalesOrderDetail objects.

To specify the query path, pass a string representation of the object graph to the Include method on the ObjectQuery. The Include method can be applied on top of any Entity Framework query: LINQ to Entities, Entity SQL, or query builder method.

The example in this topic is based on the Adventure Works Sales Model. To run the code in this topic, you must have already added the Adventure Works Sales Model to your project and configured your project to use the Entity Framework. For more information, see How to: Use the Entity Data Model Wizard (Entity Framework) or How to: Manually Configure an Entity Framework Project and How to: Manually Define an Entity Data Model (Entity Framework).

Example

This following example specifies a query path that returns a Contact and related SalesOrderHeader and SalesOrderDetail objects.

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()));
        }
    }
}

The Include method returns the query object, and you can call this method multiple times on an ObjectQuery to include entities from multiple relationships, as in the following example.

' 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");

See Also

Tasks

How to: Execute a Query that Returns Entity Type Objects
How to: Navigate Relationships Using Navigation Properties

Concepts

Loading Related Objects
Query Builder Methods
LINQ to Entities
Entity SQL Overview