DataServiceContext.LoadProperty Method

Definition

Loads deferred content from the data service.

Overloads

LoadProperty(Object, String)

Loads deferred content for a specified property from the data service.

LoadProperty(Object, String, DataServiceQueryContinuation)

Loads the next page of related entities from the data service by using the supplied query continuation object.

LoadProperty(Object, String, Uri)

Loads a page of related entities by using the supplied next link URI.

LoadProperty<T>(Object, String, DataServiceQueryContinuation<T>)

Loads the next page of related entities from the data service by using the supplied generic query continuation object.

LoadProperty(Object, String)

Loads deferred content for a specified property from the data service.

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

Parameters

entity
Object

The entity that contains the property to load.

propertyName
String

The name of the property of the specified entity to load.

Returns

The response to the load operation.

Examples

The following example shows how to explicitly load the Customers object that is related to each returned Orders instance. This example uses the DataServiceContext generated by the Add Service Reference tool based on the Northwind data service, which is created when you complete the 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

Remarks

Calling this method invokes a network operation to fetch the property value. The property specified may be any one of the properties on the entity, including properties that represent associations or links.

If the property represents an association, link or deferred property, calling this method provides the client a way to lazily load related resources.

If the entity is in the unchanged or modified state, the property value loads the related entities and marks them unchanged with unchanged links

If the property is already loaded, calling this method lets you refresh the value of the property.

Applies to

LoadProperty(Object, String, DataServiceQueryContinuation)

Loads the next page of related entities from the data service by using the supplied query continuation object.

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

Parameters

entity
Object

The entity that contains the property to load.

propertyName
String

The name of the property of the specified entity to load.

continuation
DataServiceQueryContinuation

A DataServiceQueryContinuation<T> object that represents the next page of related entities to load from the data service.

Returns

The response that contains the next page of related entity data.

Exceptions

When entity is in the Detached or Added state.

Remarks

When entity is in an Unchanged or Modified state, the related entities are loaded as objects in an Unchanged state, with links also in Unchanged state.

When entity is in a Deleted state, the related entities are loaded as objects in an Unchanged state, with links that are in the Deleted state.

Applies to

LoadProperty(Object, String, Uri)

Loads a page of related entities by using the supplied next link 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

Parameters

entity
Object

The entity that contains the property to load.

propertyName
String

The name of the property of the specified entity to load.

nextLinkUri
Uri

The URI that is used to load the next results page.

Returns

An instance of QueryOperationResponse<T> that contains the results of the request.

Exceptions

When entity is in a Detached or Added state.

Examples

This example returns related Orders entities with each Customers entity and uses a do…while loop to load Customers entities pages and a nested while loop to load pages of related Orders entities from the data service. The LoadProperty method is used to load pages of related Orders entities.

// 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

Remarks

When entity is in an Unchanged or Modified state, the related entities are loaded in the Unchanged state, and the links between the entities are also created in an Unchanged state.

When entity is in a Deleted state, the related entities are loaded in the Unchanged state, and the links between the entities are created in the Deleted state.

See also

Applies to

LoadProperty<T>(Object, String, DataServiceQueryContinuation<T>)

Loads the next page of related entities from the data service by using the supplied generic query continuation object.

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)

Type Parameters

T

Element type of collection to load.

Parameters

entity
Object

The entity that contains the property to load.

propertyName
String

The name of the property of the specified entity to load.

continuation
DataServiceQueryContinuation<T>

A DataServiceQueryContinuation<T> object that represents the next page of related entities to load from the data service.

Returns

The response that contains the next page of related entity data.

Exceptions

When entity is in the Detached or Added state.

Remarks

When entity is in an Unchanged or Modified state, the related entities are loaded as objects in an Unchanged state, with links also in Unchanged state.

When entity is in a Deleted state, the related entities are loaded as objects in an Unchanged state, with links that are in the Deleted state.

Applies to