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

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

  • LINQ to Entities

  • Entity SQL with ObjectQuery<T>

  • Query builder methods of ObjectQuery<T>

本主題的範例根據 Adventure Works Sales Model。若要執行此主題中的程式碼,您必須已經將 Adventure Works Sales Model 加入到專案中,並設定您的專案使用 Entity Framework。如需詳細資訊,請參閱 HOW TO:使用實體資料模型精靈 (Entity Framework)HOW TO:手動設定 Entity Framework 專案HOW TO:手動設定 Entity Framework 專案

範例

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

另請參閱

工作

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

概念

查詢概念模型 (Entity Framework)
定義及管理關聯性