如何:使用导航属性导航关系(实体框架)

本主题说明如何通过导航属性来导航关系。 有关更多信息,请参见导航属性。 此示例获取其姓氏为“Zhou”的联系人的所有订单。 Contact.SalesOrderHeader 导航属性用于获取每个联系人的 SalesOrderHeader 对象的集合。 将使用以下每种实体框架 查询技术演示同一示例:

  • LINQ to Entities

  • Entity SQL 以及 ObjectQuery<T>

  • ObjectQuery<T> 的查询生成器方法

本主题中的示例基于 Adventure Works 销售模型。若要运行本主题中的代码,则必须已经将 Adventure Works 销售模型添加到了您的项目中,并且已经将项目配置为使用实体框架。有关更多信息,请参见如何:使用实体数据模型向导(实体框架)如何:手动配置实体框架项目如何:手动定义实体数据模型(实体框架)

示例

这是 LINQ to Entities 示例。

Dim lastName = "Zhou"
Using context As New AdventureWorksEntities
    Dim contacts As ObjectSet(Of Contact) = context.Contacts

    Dim ordersQuery = From contact In contacts _
        Where contact.LastName = lastName _
        Select New With _
                {.LastName = contact.LastName, _
                 .Orders = contact.SalesOrderHeaders}

    For Each order In ordersQuery
        Console.WriteLine("Name: {0}", order.LastName)
        For Each orderInfo In order.Orders

            Console.WriteLine("Order ID: {0}, Order date: {1}, Total Due: {2}", _
                    orderInfo.SalesOrderID, orderInfo.OrderDate, orderInfo.TotalDue)
        Next

        Console.WriteLine("")
    Next
End Using
string lastName = "Zhou";
using (AdventureWorksEntities context = new AdventureWorksEntities())
{
    ObjectSet<Contact> contacts = context.Contacts;

    var ordersQuery = from contact in contacts
                      where contact.LastName == lastName
                      select new { LastName = contact.LastName, Orders = contact.SalesOrderHeaders };

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

这是 Entity SQL 示例。

Using context As New AdventureWorksEntities()
    Dim esqlQuery As String = "SELECT c.FirstName, c.SalesOrderHeaders " & _
        " FROM AdventureWorksEntities.Contacts AS c where c.LastName = @ln"

    Dim query As New ObjectQuery(Of DbDataRecord)(esqlQuery, context)

    ' Add parameters to the collection. 
    query.Parameters.Add(New ObjectParameter("ln", "Zhou"))

    For Each rec As DbDataRecord In query

        ' Display contact's first name. 
        Console.WriteLine("First Name {0}: ", rec(0))
        Dim list As List(Of SalesOrderHeader) = TryCast(rec(1), List(Of SalesOrderHeader))
        ' Display SalesOrderHeader information 
        ' associated with the contact. 
        For Each soh As SalesOrderHeader In list
            Console.WriteLine(" Order ID: {0}, Order date: {1}, Total Due: {2}",
                              soh.SalesOrderID, soh.OrderDate, soh.TotalDue)
        Next
    Next
End Using
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    string esqlQuery = @"SELECT c.FirstName, c.SalesOrderHeaders 
        FROM AdventureWorksEntities.Contacts AS c where c.LastName = @ln";
    ObjectQuery<DbDataRecord> query = new ObjectQuery<DbDataRecord>(esqlQuery, context);
    query.Parameters.Add(new ObjectParameter("ln", "Zhou"));

    foreach (DbDataRecord rec in query)
    {

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

这是查询生成器方法示例。

Dim lastName = "Zhou"

Using context As New AdventureWorksEntities()
    ' Define a query that returns a nested 
    ' DbDataRecord for the projection. 
    Dim query As ObjectQuery(Of DbDataRecord) = context.Contacts.Select("it.FirstName, it.LastName, it.SalesOrderHeaders") _
                                                .Where("it.LastName = @ln", New ObjectParameter("ln", lastName))

    For Each rec As DbDataRecord In query.Execute(MergeOption.AppendOnly)

        ' Display contact's first name. 
        Console.WriteLine("First Name {0}: ", rec(0))
        Dim list As List(Of SalesOrderHeader) = TryCast(rec(2), List(Of SalesOrderHeader))
        ' Display SalesOrderHeader information 
        ' associated with the contact. 
        For Each soh As SalesOrderHeader In list
            Console.WriteLine(" Order ID: {0}, Order date: {1}, Total Due: {2}", _
                              soh.SalesOrderID, soh.OrderDate, soh.TotalDue)
        Next
    Next
End Using
string lastName = "Zhou";
using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    // Define a query that returns a nested 
    // DbDataRecord for the projection.
    ObjectQuery<DbDataRecord> query =
        context.Contacts.Select("it.FirstName, "
            + "it.LastName, it.SalesOrderHeaders")
        .Where("it.LastName = @ln", new ObjectParameter("ln", lastName));

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

另请参见

任务

如何:使用查询路径调整结果(实体框架)

概念

查询概念模型(实体框架)
定义和管理关系(实体框架)