HOW TO:在 EntityCollection 中查詢相關物件 (Entity Framework)
本主題提供的範例將示範如何在關聯性導覽屬性所傳回的 EntityCollection 中查詢相關物件。
本主題的範例是根據 Adventure Works Sales Model。若要執行此範例中的程式碼,您必須已經將 AdventureWorks Sales Model 加入到專案中,並設定您的專案使用 Entity Framework 。若要這樣做,請完成 HOW TO:手動設定 Entity Framework 專案和 HOW TO:手動定義模型和對應檔 (Entity Framework) 中的程序。您也可以使用 [Entity Data Model 精靈] 定義 AdventureWorks Sales Model。如需詳細資訊,請參閱 HOW TO:使用實體資料模型精靈 (Entity Framework)。
範例
這個範例會載入與特定連絡人有關之 SalesOrderHeader 物件的集合,然後使用 LINQ 運算式來傳回已經出貨之線上訂單的清單。
' Specify the customer ID.
Dim customerId As Integer = 4332
Using context As New AdventureWorksEntities()
' Get a specified customer by contact ID.
Dim customer = (From customers In context.Contacts _
Where customers.ContactID = customerId _
Select customers).First()
' You do not have to call the Load method to load the orders for the customer,
' because lazy loading is set to true
' by the constructor of the AdventureWorksEntities object.
' With lazy loading set to true the related objects are loaded when
' you access the navigation property. In this case SalesOrderHeaders.
' Write the number of orders for the customer.
Console.WriteLine("Customer '{0}' has placed {1} total orders.", _
customer.LastName, customer.SalesOrderHeaders.Count)
' Get the online orders that have shipped.
Dim shippedOrders = From order In customer.SalesOrderHeaders _
Where order.OnlineOrderFlag = True AndAlso order.Status = 5 _
Select order
' Write the number of orders placed online.
Console.WriteLine("{0} orders placed online have been shipped.", shippedOrders.Count())
End Using
// Specify the customer ID.
int customerId = 4332;
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
// Get a specified customer by contact ID.
var customer = (from customers in context.Contacts
where customers.ContactID == customerId
select customers).First();
// You do not have to call the Load method to load the orders for the customer,
// because lazy loading is set to true
// by the constructor of the AdventureWorksEntities object.
// With lazy loading set to true the related objects are loaded when
// you access the navigation property. In this case SalesOrderHeaders.
// Write the number of orders for the customer.
Console.WriteLine("Customer '{0}' has placed {1} total orders.",
customer.LastName, customer.SalesOrderHeaders.Count);
// Get the online orders that have shipped.
var shippedOrders =
from order in customer.SalesOrderHeaders
where order.OnlineOrderFlag == true
&& order.Status == 5
select order;
// Write the number of orders placed online.
Console.WriteLine("{0} orders placed online have been shipped.",
shippedOrders.Count());
}
此範例會針對 SalesOrderHeader 物件的集合使用相同的 LINQ 查詢來當做第一個範例。CreateSourceQuery 方法是用來只載入查詢所傳回的物件,而不會在一開始將所有相關的物件載入集合中。然後會在 SalesOrderHeader 關聯性導覽屬性所傳回的 EntityCollection 上呼叫 Load 方法。
' Specify the customer ID.
Dim customerId As Integer = 4332
Using context As New AdventureWorksEntities()
' Get a specified customer by contact ID.
Dim customer = (From customers In context.Contacts
Where customers.ContactID = customerId
Select customers).First()
' Use CreateSourceQuery to generate a query that returns
' only the online orders that have shipped.
Dim shippedOrders = From orders In customer.SalesOrderHeaders.CreateSourceQuery() _
Where orders.OnlineOrderFlag = True AndAlso orders.Status = 5 _
Select orders
' Write the number of orders placed online.
Console.WriteLine("{0} orders placed online have been shipped.", shippedOrders.Count())
' You do not have to call the Load method to load the orders for the customer,
' because lazy loading is set to true
' by the constructor of the AdventureWorksEntities object.
' With lazy loading set to true the related objects are loaded when
' you access the navigation property. In this case SalesOrderHeaders.
' Write the number of total orders for the customer.
Console.WriteLine("Customer '{0}' has placed {1} total orders.", _
customer.LastName, customer.SalesOrderHeaders.Count)
End Using
// Specify the customer ID.
int customerId = 4332;
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
// Get a specified customer by contact ID.
var customer = (from customers in context.Contacts
where customers.ContactID == customerId
select customers).First();
// Use CreateSourceQuery to generate a query that returns
// only the online orders that have shipped.
var shippedOrders =
from orders in customer.SalesOrderHeaders.CreateSourceQuery()
where orders.OnlineOrderFlag == true
&& orders.Status == 5
select orders;
// Write the number of orders placed online.
Console.WriteLine("{0} orders placed online have been shipped.",
shippedOrders.Count());
// You do not have to call the Load method to load the orders for the customer,
// because lazy loading is set to true
// by the constructor of the AdventureWorksEntities object.
// With lazy loading set to true the related objects are loaded when
// you access the navigation property. In this case SalesOrderHeaders.
// Write the number of total orders for the customer.
Console.WriteLine("Customer '{0}' has placed {1} total orders.",
customer.LastName, customer.SalesOrderHeaders.Count);
}
另請參閱
工作
HOW TO:執行傳回實體類型物件的查詢 (Entity Framework)
HOW TO:使用查詢路徑來設定結果外觀 (Entity Framework)
HOW TO:使用導覽屬性巡覽關聯性 (Entity Framework)