DataServiceContext.LoadProperty 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从数据服务加载延迟的内容。
重载
LoadProperty(Object, String) |
从数据服务加载指定属性的延迟内容。 |
LoadProperty(Object, String, DataServiceQueryContinuation) |
通过使用所提供的查询延续对象,从数据服务加载下一页相关实体。 |
LoadProperty(Object, String, Uri) |
通过使用所提供的下一链接 URI 来加载相关实体页。 |
LoadProperty<T>(Object, String, DataServiceQueryContinuation<T>) |
通过使用所提供的通用查询延续对象,从数据服务加载下一页相关实体。 |
LoadProperty(Object, String)
从数据服务加载指定属性的延迟内容。
public:
System::Data::Services::Client::QueryOperationResponse ^ LoadProperty(System::Object ^ entity, System::String ^ propertyName);
public System.Data.Services.Client.QueryOperationResponse LoadProperty (object entity, string propertyName);
member this.LoadProperty : obj * string -> System.Data.Services.Client.QueryOperationResponse
Public Function LoadProperty (entity As Object, propertyName As String) As QueryOperationResponse
参数
- entity
- Object
包含要加载的属性的实体。
- propertyName
- String
要加载的指定实体的属性名称。
返回
对加载操作的响应。
示例
以下示例演示如何显式加载与每个返回的 Customers
实例相关的 Orders
对象。 此示例使用DataServiceContext基于 Northwind 数据服务的“添加服务引用”工具生成的 ,该服务是在完成WCF Data Services 时创建的。
// 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);
}
' 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
注解
调用此方法时,将调用网络操作以提取属性值。 指定的属性可以是实体的任一属性,包括表示关联或链接的属性。
如果属性是表示关联、链接或延迟的属性,则调用此方法时,会向客户端提供延迟加载相关资源的方式。
如果实体处于未更改或已修改状态,则属性值将加载相关实体,并将这些实体与未更改链接一起标记为“未更改”
如果已加载属性,则调用此方法可让您刷新该属性的值。
适用于
LoadProperty(Object, String, DataServiceQueryContinuation)
通过使用所提供的查询延续对象,从数据服务加载下一页相关实体。
public:
System::Data::Services::Client::QueryOperationResponse ^ LoadProperty(System::Object ^ entity, System::String ^ propertyName, System::Data::Services::Client::DataServiceQueryContinuation ^ continuation);
public System.Data.Services.Client.QueryOperationResponse LoadProperty (object entity, string propertyName, System.Data.Services.Client.DataServiceQueryContinuation continuation);
member this.LoadProperty : obj * string * System.Data.Services.Client.DataServiceQueryContinuation -> System.Data.Services.Client.QueryOperationResponse
Public Function LoadProperty (entity As Object, propertyName As String, continuation As DataServiceQueryContinuation) As QueryOperationResponse
参数
- entity
- Object
包含要加载的属性的实体。
- propertyName
- String
要加载的指定实体的属性名称。
- continuation
- DataServiceQueryContinuation
DataServiceQueryContinuation<T> 对象,表示要从数据服务加载的下一页相关实体。
返回
包含下一页相关实体数据的响应。
例外
注解
当 entity
处于 Unchanged 或 Modified 状态时,在 Unchanged 状态下将相关实体作为对象进行加载,此时链接也处于 Unchanged 状态。
当 entity
处于 Deleted 状态时,在 Unchanged 状态下将相关实体作为对象进行加载,此时链接也处于 Deleted 状态。
适用于
LoadProperty(Object, String, Uri)
通过使用所提供的下一链接 URI 来加载相关实体页。
public:
System::Data::Services::Client::QueryOperationResponse ^ LoadProperty(System::Object ^ entity, System::String ^ propertyName, Uri ^ nextLinkUri);
public System.Data.Services.Client.QueryOperationResponse LoadProperty (object entity, string propertyName, Uri nextLinkUri);
member this.LoadProperty : obj * string * Uri -> System.Data.Services.Client.QueryOperationResponse
Public Function LoadProperty (entity As Object, propertyName As String, nextLinkUri As Uri) As QueryOperationResponse
参数
- entity
- Object
包含要加载的属性的实体。
- propertyName
- String
要加载的指定实体的属性名称。
- nextLinkUri
- Uri
用于加载下一结果页的 URI。
返回
包含请求结果的 QueryOperationResponse<T> 实例。
例外
示例
下面的示例返回每个 Orders
实体的相关 Customers
实体,并使用 do…while
循环从数据服务中加载 Customers
实体页以及使用嵌套的 while
循环从数据服务中加载相关的 Orders
实体的页。
LoadProperty 方法用于加载相关的 Orders
实体页。
// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);
DataServiceQueryContinuation<Customer> nextLink = null;
int pageCount = 0;
int innerPageCount = 0;
try
{
// Execute the query for all customers and related orders,
// and get the response object.
var response =
context.Customers.AddQueryOption("$expand", "Orders")
.Execute() as QueryOperationResponse<Customer>;
// With a paged response from the service, use a do...while loop
// to enumerate the results before getting the next link.
do
{
// Write the page number.
Console.WriteLine("Customers Page {0}:", ++pageCount);
// If nextLink is not null, then there is a new page to load.
if (nextLink != null)
{
// Load the new page from the next link URI.
response = context.Execute<Customer>(nextLink)
as QueryOperationResponse<Customer>;
}
// Enumerate the customers in the response.
foreach (Customer c in response)
{
Console.WriteLine("\tCustomer Name: {0}", c.CompanyName);
Console.WriteLine("\tOrders Page {0}:", ++innerPageCount);
// Get the next link for the collection of related Orders.
DataServiceQueryContinuation<Order> nextOrdersLink =
response.GetContinuation(c.Orders);
while (nextOrdersLink != null)
{
foreach (Order o in c.Orders)
{
// Print out the orders.
Console.WriteLine("\t\tOrderID: {0} - Freight: ${1}",
o.OrderID, o.Freight);
}
// Load the next page of Orders.
var ordersResponse = context.LoadProperty(c, "Orders", nextOrdersLink);
nextOrdersLink = ordersResponse.GetContinuation();
}
}
}
// Get the next link, and continue while there is a next link.
while ((nextLink = response.GetContinuation()) != null);
}
catch (DataServiceQueryException ex)
{
throw new ApplicationException(
"An error occurred during query execution.", ex);
}
' Create the DataServiceContext using the service URI.
Dim context = New NorthwindEntities(svcUri)
Dim nextLink As DataServiceQueryContinuation(Of Customer) = Nothing
Dim pageCount = 0
Dim innerPageCount = 0
Try
' Execute the query for all customers and related orders,
' and get the response object.
Dim response = _
CType(context.Customers.AddQueryOption("$expand", "Orders") _
.Execute(), QueryOperationResponse(Of Customer))
' With a paged response from the service, use a do...while loop
' to enumerate the results before getting the next link.
Do
' Write the page number.
Console.WriteLine("Customers Page {0}:", ++pageCount)
' If nextLink is not null, then there is a new page to load.
If nextLink IsNot Nothing Then
' Load the new page from the next link URI.
response = CType(context.Execute(Of Customer)(nextLink), _
QueryOperationResponse(Of Customer))
End If
' Enumerate the customers in the response.
For Each c As Customer In response
Console.WriteLine(vbTab & "Customer Name: {0}", c.CompanyName)
Console.WriteLine(vbTab & "Orders Page {0}:", innerPageCount + 1)
' Get the next link for the collection of related Orders.
Dim nextOrdersLink As DataServiceQueryContinuation(Of Order) = _
response.GetContinuation(c.Orders)
While nextOrdersLink IsNot Nothing
For Each o As Order In c.Orders
' Print out the orders.
Console.WriteLine(vbTab & vbTab & "OrderID: {0} - Freight: ${1}", _
o.OrderID, o.Freight)
Next
' Load the next page of Orders.
Dim ordersResponse = _
context.LoadProperty(c, "Orders", nextOrdersLink)
nextOrdersLink = ordersResponse.GetContinuation()
End While
Next
' Get the next link, and continue while there is a next link.
nextLink = response.GetContinuation()
Loop While nextLink IsNot Nothing
Catch ex As DataServiceQueryException
Throw New ApplicationException( _
"An error occurred during query execution.", ex)
End Try
注解
当 entity
处于 Unchanged 或 Modified 状态时,在 Unchanged 状态下加载相关实体,并且也在 Unchanged 状态下创建各实体之间的链接。
当 entity
处于 Deleted 状态时,在 Unchanged 状态下加载相关实体,并在 Deleted 状态下创建各实体之间的链接。
另请参阅
适用于
LoadProperty<T>(Object, String, DataServiceQueryContinuation<T>)
通过使用所提供的通用查询延续对象,从数据服务加载下一页相关实体。
public:
generic <typename T>
System::Data::Services::Client::QueryOperationResponse<T> ^ LoadProperty(System::Object ^ entity, System::String ^ propertyName, System::Data::Services::Client::DataServiceQueryContinuation<T> ^ continuation);
public System.Data.Services.Client.QueryOperationResponse<T> LoadProperty<T> (object entity, string propertyName, System.Data.Services.Client.DataServiceQueryContinuation<T> continuation);
member this.LoadProperty : obj * string * System.Data.Services.Client.DataServiceQueryContinuation<'T> -> System.Data.Services.Client.QueryOperationResponse<'T>
Public Function LoadProperty(Of T) (entity As Object, propertyName As String, continuation As DataServiceQueryContinuation(Of T)) As QueryOperationResponse(Of T)
类型参数
- T
要加载的集合的元素类型。
参数
- entity
- Object
包含要加载的属性的实体。
- propertyName
- String
要加载的指定实体的属性名称。
- continuation
- DataServiceQueryContinuation<T>
DataServiceQueryContinuation<T> 对象,表示要从数据服务加载的下一页相关实体。
返回
包含下一页相关实体数据的响应。
例外
注解
当 entity
处于 Unchanged 或 Modified 状态时,在 Unchanged 状态下将相关实体作为对象进行加载,此时链接也处于 Unchanged 状态。
当 entity
处于 Deleted 状态时,在 Unchanged 状态下将相关实体作为对象进行加载,此时链接也处于 Deleted 状态。