HOW TO:使用查詢路徑來設定結果外觀 (Entity Framework)
本主題提供的範例將示範如何指定查詢路徑,以便定義在概念模型中查詢特定物件時所傳回的相關物件。 這個範例中的查詢會傳回單一 Contact 物件,以及所有相關的 SalesOrderHeader 和 SalesOrderDetail 物件。
如果要指定查詢路徑,請將代表物件圖形的字串傳遞給 ObjectQuery 上的 Include 方法。 Include 方法可以套用在任何 Entity Framework 查詢的最上層:LINQ to Entities 、Entity SQL 或查詢產生器方法。
本主題的範例根據 Adventure Works Sales Model。若要執行此主題中的程式碼,您必須已經將 Adventure Works Sales Model 加入到專案中,並設定您的專案使用 Entity Framework。如需詳細資訊,請參閱 HOW TO:使用實體資料模型精靈 (Entity Framework) 或 HOW TO:手動設定 Entity Framework 專案及 HOW TO:手動設定 Entity Framework 專案。
範例
下列範例會指定查詢路徑,此路徑會傳回 Contact 及相關的 SalesOrderHeader 和 SalesOrderDetail 物件。
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()));
}
}
}
Include 方法會傳回查詢物件,而且您可以在 ObjectQuery 上多次呼叫這個方法,從多個關聯性納入實體,如下列範例所示。
' 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");
另請參閱
工作
HOW TO:執行傳回實體類型物件的查詢 (Entity Framework)
HOW TO:使用導覽屬性巡覽關聯性 (Entity Framework)
概念
載入相關的物件 (Entity Framework)
查詢產生器方法 (Entity Framework)
LINQ to Entities
Entity SQL 概觀