แก้ไข

แชร์ผ่าน


DataServiceContext.Execute Method

Definition

Sends a request to the data service to execute a specific URI.

Overloads

Execute<T>(DataServiceQueryContinuation<T>)

Sends a request to the data service to retrieve the next page of data in a paged query result.

Execute<TElement>(Uri)

Sends a request to the data service to execute a specific URI.

Execute<T>(DataServiceQueryContinuation<T>)

Sends a request to the data service to retrieve the next page of data in a paged query result.

public:
generic <typename T>
 System::Data::Services::Client::QueryOperationResponse<T> ^ Execute(System::Data::Services::Client::DataServiceQueryContinuation<T> ^ continuation);
public System.Data.Services.Client.QueryOperationResponse<T> Execute<T> (System.Data.Services.Client.DataServiceQueryContinuation<T> continuation);
member this.Execute : System.Data.Services.Client.DataServiceQueryContinuation<'T> -> System.Data.Services.Client.QueryOperationResponse<'T>
Public Function Execute(Of T) (continuation As DataServiceQueryContinuation(Of T)) As QueryOperationResponse(Of T)

Type Parameters

T

The type returned by the query.

Parameters

continuation
DataServiceQueryContinuation<T>

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

Returns

The response that contains the next page of data in the query result.

Exceptions

When an error is raised either during execution of the request or when it converts the contents of the response message into objects.

Remarks

The supplied DataServiceQueryContinuation<T> object contains the URI that, when executed, returns the next page of data in the query result.

Applies to

Execute<TElement>(Uri)

Sends a request to the data service to execute a specific URI.

public:
generic <typename TElement>
 System::Collections::Generic::IEnumerable<TElement> ^ Execute(Uri ^ requestUri);
public System.Collections.Generic.IEnumerable<TElement> Execute<TElement> (Uri requestUri);
member this.Execute : Uri -> seq<'Element>
Public Function Execute(Of TElement) (requestUri As Uri) As IEnumerable(Of TElement)

Type Parameters

TElement

The type that the query returns.

Parameters

requestUri
Uri

The URI to which the query request will be sent. The URI may be any valid data service URI. Can contain $ query parameters.

Returns

IEnumerable<TElement>

The results of the query operation.

Exceptions

When a response is not received from a request to the requestUri.

When requestUri is null.

When requestUri is not a valid URI for the data service.

When an error is raised either during execution of the request or when it converts the contents of the response message into objects.

The data service returns an HTTP 404: Resource Not Found error.

Examples

This example uses a do…while loop to load Customers entities from a paged results from the data service. The Execute method is called by using the next link URI to receive the next page of data.

// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);
DataServiceQueryContinuation<Customer> token = null;
int pageCount = 0;

try
{
    // Execute the query for all customers and get the response object.
    QueryOperationResponse<Customer> response =
        context.Customers.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("Page {0}:", pageCount++);

        // If nextLink is not null, then there is a new page to load.
        if (token != null)
        {
            // Load the new page from the next link URI.
            response = context.Execute<Customer>(token)
                as QueryOperationResponse<Customer>;
        }

        // Enumerate the customers in the response.
        foreach (Customer customer in response)
        {
            Console.WriteLine("\tCustomer Name: {0}", customer.CompanyName);
        }
    }

    // Get the next link, and continue while there is a next link.
    while ((token = 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 token As DataServiceQueryContinuation(Of Customer) = Nothing
Dim pageCount = 0

Try
    ' Execute the query for all customers and get the response object.
    Dim response As QueryOperationResponse(Of Customer) = _
        CType(context.Customers.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("Page {0}:", pageCount + 1)

        ' If nextLink is not null, then there is a new page to load.
        If token IsNot Nothing Then
            ' Load the new page from the next link URI.
            response = CType(context.Execute(Of Customer)(token),  _
            QueryOperationResponse(Of Customer))
        End If

        ' Enumerate the customers in the response.
        For Each customer As Customer In response
            Console.WriteLine(vbTab & "Customer Name: {0}", customer.CompanyName)
        Next

        ' Get the next link, and continue while there is a next link.
        token = response.GetContinuation()
    Loop While token IsNot Nothing
Catch ex As DataServiceQueryException
    Throw New ApplicationException( _
            "An error occurred during query execution.", ex)
End Try

Remarks

The Execute method is used to query a data service by URI; the method causes an HTTP GET request to be issued to the data service. The request URI specified can be absolute or relative.

If the requestUri is absolute, this method validates whether the URI points to the same data service that was specified when constructing the DataServiceContext. If the requestUri is relative, this method strips off any leading slashes and appends requestUri to what was provided when constructing the DataServiceContext. A slash is appended after the URI passed to the DataServiceContext constructor, if one is not already present.

When this method returns, all of the HTTP response for the request has been read from the network stream, but the response will not have been processed; there is no identity resolution or object materialization. Identity resolution and full object materialization do not occur for a specified entity in the response until it is enumerated.

See also

Applies to