Share via


DataServiceContext.LoadProperty 方法

定義

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

多載

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 數據服務使用 Add Service Reference 工具所產生的 ,當您完成 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 處於 DetachedAdded 狀態時。

備註

entity 處於 UnchangedModified 狀態時,相關實體是以 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> 執行個體。

例外狀況

entity 處於 DetachedAdded 狀態時。

範例

本範例會隨每個 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 處於 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 狀態。

entity 處於 Deleted 狀態時,相關實體是以 Unchanged 狀態的物件形式載入,而連結則是處於 Deleted 狀態。

適用於