HOW TO:載入相關實體 (WCF Data Services)
當您需要在 WCF Data Services 中載入相關的實體時,可以在 DataServiceContext 類別上使用 LoadProperty 方法。 您也可以在 DataServiceQuery<TElement> 使用 Expand 方法,要求立即將相關實體載入至同一個查詢回應中。
本主題的範例使用 Northwind 範例資料服務,以及自動產生的用戶端資料服務類別。 此服務和用戶端資料類別會在您完成 WCF Data Services 快速入門時建立。
範例
下列範例示範如何明確地載入與所傳回之每個 Orders 執行個體相關的 Customer。
' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)
Try
' Enumerate over the top 10 orders obtained from the context.
For Each order As Order In context.Orders.Take(10)
' Explicitly load the customer for each order.
context.LoadProperty(order, "Customer")
' Write out customer and order information.
Console.WriteLine("Customer: {0} - Order ID: {1}", _
order.Customer.CompanyName, order.OrderID)
Next
Catch ex As DataServiceQueryException
Throw New ApplicationException( _
"An error occurred during query execution.", ex)
End Try
// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);
try
{
// Enumerate over the top 10 orders obtained from the context.
foreach (Order order in context.Orders.Take(10))
{
// Explicitly load the customer for each order.
context.LoadProperty(order, "Customer");
// Write out customer and order information.
Console.WriteLine("Customer: {0} - Order ID: {1}",
order.Customer.CompanyName, order.OrderID);
}
}
catch (DataServiceQueryException ex)
{
throw new ApplicationException(
"An error occurred during query execution.", ex);
}
下列範例示範如何使用 Expand 方法傳回屬於查詢所傳回之 Orders 的 Order Details 方法。
' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)
' Define a query for orders that also returns items and customers.
Dim query As DataServiceQuery(Of Order) = _
context.Orders.Expand("Order_Details,Customer")
Try
' Enumerate over the first 10 results of the query.
For Each order As Order In query.Take(10)
Console.WriteLine("Customer: {0}", order.Customer.CompanyName)
Console.WriteLine("Order ID: {0}", order.OrderID)
For Each item As Order_Detail In order.Order_Details
Console.WriteLine(vbTab & "Product: {0} - Quantity: {1}", _
item.ProductID, item.Quantity)
Next
Next
Catch ex As DataServiceQueryException
Throw New ApplicationException( _
"An error occurred during query execution.", ex)
End Try
// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);
// Define a query for orders that also returns items and customers.
DataServiceQuery<Order> query =
context.Orders.Expand("Order_Details,Customer");
try
{
// Enumerate over the first 10 results of the query.
foreach (Order order in query.Take(10))
{
Console.WriteLine("Customer: {0}", order.Customer.CompanyName);
Console.WriteLine("Order ID: {0}", order.OrderID);
foreach (Order_Detail item in order.Order_Details)
{
Console.WriteLine("\tProduct: {0} - Quantity: {1}",
item.ProductID, item.Quantity);
}
}
}
catch (DataServiceQueryException ex)
{
throw new ApplicationException(
"An error occurred during query execution.", ex);
}