共用方式為


HOW TO:使用導覽屬性巡覽關聯性 (Entity Framework)

本主題將示範如何透過導覽屬性巡覽關聯性。如需詳細資訊,請參閱導覽屬性 (EDM)。此範例會取得姓氏為 "Zhou" 之連絡人的所有訂單。Contact.SalesOrderHeader 導覽屬性是用來取得每一個連絡人之 SalesOrderHeader 物件的集合。也會顯示使用下列每一個 實體架構 查詢技術的相同範例:

  • LINQ to Entities

  • 使用 ObjectQuery<T> 的 Entity SQL

  • ObjectQuery<T> 的查詢產生器方法

本主題的範例是根據 Adventure Works Sales Model。若要執行此範例中的程式碼,您必須已經將 AdventureWorks Sales Model 加入到專案中,並設定您的專案使用 實體架構。若要這樣做,請完成 HOW TO:手動設定 Entity Framework 專案HOW TO:以手動方式定義 Entity Data Model (Entity Framework) 中的程序。您也可以使用 [Entity Data Model 精靈] 來定義 AdventureWorks Sales Model。如需詳細資訊,請參閱 HOW TO:使用 Entity Data Model 精靈 (Entity Framework)

範例

這是 LINQ 到實體 範例。

Using AWEntities As New AdventureWorksEntities
    Dim contacts As ObjectQuery(Of Contact) = AWEntities.Contact

    Dim ordersQuery = From contact In contacts _
        Where contact.LastName = "Zhou" _
        Select New With _
                {.LastName = contact.LastName, _
                 .Orders = contact.SalesOrderHeader}

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

這是 實體 SQL 範例。

Using advWorksContext As New AdventureWorksEntities
    Dim esqlQuery As String = "SELECT c.FirstName, c.SalesOrderHeader " & _
            " FROM AdventureWorksEntities.Contact AS c where c.LastName = 'Zhou'"
    Try
        Dim objQuery As New ObjectQuery(Of DbDataRecord)(esqlQuery, advWorksContext)
        For Each rec As DbDataRecord In objQuery
            ' Display contact's first name.
            Console.WriteLine("First Name {0}: ", rec.Item(0))
            Dim list As List(Of SalesOrderHeader) = DirectCast(rec.Item(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
    Catch ex As EntityException
        Console.WriteLine(ex.ToString())
    Catch ex As InvalidOperationException
        Console.WriteLine(ex.ToString())
    End Try
End Using
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());
    }
}

這是查詢產生器方法範例。

Using advWorksContext As New AdventureWorksEntities()
    Try
        ' Define a query that returns a nested 
        ' DbDataRecord for the projection.
        Dim query As ObjectQuery(Of DbDataRecord) = _
            advWorksContext.Contact.Select("it.FirstName, " _
                + "it.LastName, it.SalesOrderHeader") _
            .Where("it.LastName = 'Zhou'")

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

            ' Display contact's first name.
            Console.WriteLine("First Name {0}: ", rec(0))

            ' Display SalesOrderHeader information 
            ' associated with the contact.
            For Each soh As SalesOrderHeader In CType(rec(2), List(Of SalesOrderHeader))
                Console.WriteLine("   Order ID: {0}, " + _
                    "Order date: {1}, Total Due: {2}", _
                    soh.SalesOrderID, soh.OrderDate, soh.TotalDue)
            Next
        Next
    Catch ex As EntitySqlException
        Console.WriteLine(ex.ToString())
    End Try
End Using
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());
    }
}

另請參閱

工作

HOW TO:使用查詢路徑來設定結果外觀 (Entity Framework)

概念

Entity Data Model 關聯性

其他資源

查詢 Entity Data Model (Entity Framework 工作)