Export (0) Print
Expand All

How to: Navigate Relationships Using Navigation Properties (Entity Framework)

This topic shows how to navigate relationships through navigation properties. For more information, see Navigation Properties (EDM). The example gets all the orders of the contacts whose last name is "Zhou". The Contact.SalesOrderHeader navigation property is used to get the collection of SalesOrderHeader objects for each contact. The same example is shown using each of the following Entity Framework query technologies:

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

Example

This is the LINQ to Entities example.

using (AdventureWorksEntities AWEntities = new AdventureWorksEntities())
{
    ObjectQuery<Contact> contacts = AWEntities.Contact;

    var ordersQuery = from contact in contacts
                      where contact.LastName == "Zhou"
                      select new { LastName = contact.LastName, Orders = contact.SalesOrderHeader };

    foreach (var order in ordersQuery)
    {
        Console.WriteLine("Name: {0}", order.LastName);
        foreach (SalesOrderHeader orderInfo in order.Orders)
        {
            Console.WriteLine("Order ID: {0}, Order date: {1}, Total Due: {2}",
                orderInfo.SalesOrderID, orderInfo.OrderDate, orderInfo.TotalDue);
        }
        Console.WriteLine("");
    }
}

This is the Entity SQL example.


using (AdventureWorksEntities advWorksContext =
    new AdventureWorksEntities())
{
    string esqlQuery = @"SELECT c.FirstName, c.SalesOrderHeader 
        FROM AdventureWorksEntities.Contact AS c where c.LastName = 'Zhou'";

    try
    {
        foreach (DbDataRecord rec in
            new ObjectQuery<DbDataRecord>(esqlQuery, advWorksContext))
        {

            // Display contact's first name.
            Console.WriteLine("First Name {0}: ", rec[0]);
            List<SalesOrderHeader> list = rec[1] as List<SalesOrderHeader>;
            // Display SalesOrderHeader information 
            // associated with the contact.
            foreach (SalesOrderHeader soh in list)
            {
                Console.WriteLine("   Order ID: {0}, Order date: {1}, Total Due: {2}",
                    soh.SalesOrderID, soh.OrderDate, soh.TotalDue);
            }
        }
    }
    catch (EntityException ex)
    {
        Console.WriteLine(ex.ToString());
    }
    catch (InvalidOperationException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

This is the query builder method example.

using (AdventureWorksEntities advWorksContext =
    new AdventureWorksEntities())
{
    try
    {
        // Define a query that returns a nested 
        // DbDataRecord for the projection.
        ObjectQuery<DbDataRecord> query =
            advWorksContext.Contact.Select("it.FirstName, "
                + "it.LastName, it.SalesOrderHeader")
            .Where("it.LastName = 'Zhou'");

        foreach (DbDataRecord rec in 
            query.Execute(MergeOption.AppendOnly))
        {

            // Display contact's first name.
            Console.WriteLine("First Name {0}: ", rec[0]);
            List<SalesOrderHeader> list = rec[2] 
                as List<SalesOrderHeader>;
            // Display SalesOrderHeader information 
            // associated with the contact.
            foreach (SalesOrderHeader soh in list)
            {
                Console.WriteLine("   Order ID: {0}, " +
                    "Order date: {1}, Total Due: {2}",
                    soh.SalesOrderID, soh.OrderDate, soh.TotalDue);
            }
        }
    }
    catch (EntitySqlException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

See Also

Community Additions

Show:
© 2015 Microsoft