DataServiceContext.LoadProperty 方法

定義

從資料服務載入延遲內容。

多載

名稱 Description
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 實例相關的物件。 此範例使用由 Add Service Reference 工具產生的, DataServiceContext 該工具基於 Northwind 資料服務,該服務會在完成 WCF 資料服務時建立。

// 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 進入 DetachedAdded 州。

備註

entity 處於 UnchangedModified 狀態時,相關實體會以物件形式載入該 Unchanged 狀態,連結也同樣處於 Unchanged 狀態。

當 處於狀態entityDeleted,相關實體會以物件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> 包含請求的結果。

例外狀況

何時entity處於或DetachedAdded州。

範例

此範例會回傳每個實體相關的OrdersCustomers實體,並使用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 處於 UnchangedModified 狀態時,相關實體會載入該 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 進入 DetachedAdded 州。

備註

entity 處於 UnchangedModified 狀態時,相關實體會以物件形式載入該 Unchanged 狀態,連結也同樣處於 Unchanged 狀態。

當 處於狀態entityDeleted,相關實體會以物件Unchanged載入,並以Deleted連結為該狀態。

適用於