ExecuteBatch Method

DataServiceContext.ExecuteBatch Method

Submits a group of queries as a batch to the data service.

Namespace:  System.Data.Services.Client
Assembly:  System.Data.Services.Client (in System.Data.Services.Client.dll)

public DataServiceResponse ExecuteBatch(
	params DataServiceRequest[] queries
)

Parameters

queries
Type: System.Data.Services.Client.DataServiceRequest[]
Array of DataServiceRequest[] objects that make up the queries.

Return Value

Type: System.Data.Services.Client.DataServiceResponse
The response to the batch operation.

The queries are specified as DataServiceRequest<TElement> instances. Returns a DataServiceResponse that represents the response of the batch request as a whole. Individual query responses are represented as individual OperationResponse objects that can be accessed by enumerating the DataServiceResponse instance.

When this method returns the whole HTTP response for the batch request has been read from the network stream, but the responses have not been processed. No identity resolution or object materialization occurs for a specified entity in the response until it is enumerated.

The following example shows how to call the ExecuteBatch() method to execute an array of DataServiceRequest<TElement> objects that contains queries that return both Customers and Products objects from the Northwind data service. The collection of QueryOperationResponse<T> objects in the returned DataServiceResponse is enumerated, and the collection of objects that is contained in each QueryOperationResponse<T> is also enumerated. 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 quickstart.


string customerId = "ALFKI";

// Create the DataServiceContext using the service URI.
NorthwindEntities context = new NorthwindEntities(svcUri);

// Create the separate query URI's, one that returns 
// a single customer and another that returns all Products.
Uri customerUri = new Uri(svcUri.AbsoluteUri + 
    "/Customers('" + customerId + "')/?$expand=Orders");
Uri productsUri = new Uri(svcUri.AbsoluteUri +
   "/Products");

// Create the query requests.
DataServiceRequest<Customer> customerQuery = 
    new DataServiceRequest<Customer>(customerUri);
DataServiceRequest<Product> productsQuery =
                new DataServiceRequest<Product>(productsUri);

// Add the query requests to a batch request array.
DataServiceRequest[] batchRequests = 
    new DataServiceRequest[]{customerQuery, productsQuery};

DataServiceResponse batchResponse;

try
{
    // Execute the query batch and get the response.
    batchResponse = context.ExecuteBatch(batchRequests);

    if (batchResponse.IsBatchResponse)
    {
        // Parse the batchResponse.BatchHeaders.
    }  
    // Enumerate over the results of the query.
    foreach (QueryOperationResponse response in batchResponse)
    {
        // Handle an error response.
        if (response.StatusCode > 299 || response.StatusCode < 200)
        {
            Console.WriteLine("An error occurred.");
            Console.WriteLine(response.Error.Message);
        }
        else
        {
            // Find the response for the Customers query.
            if (response.Query.ElementType == typeof(Customer))
            {
                foreach (Customer customer in response)
                {
                    Console.WriteLine("Customer: {0}", customer.CompanyName);
                    foreach (Order order in customer.Orders)
                    {
                        Console.WriteLine("Order ID: {0} - Freight: {1}",
                            order.OrderID, order.Freight);
                    }
                }
            }
            // Find the response for the Products query.
            else if (response.Query.ElementType == typeof(Product))
            {
                foreach (Product product in response)
                {
                    Console.WriteLine("Product: {0}", product.ProductName);
                }
            }
        }
    }
}

// This type of error is raised when the data service returns with
// a response code < 200 or >299 in the top level element.
catch (DataServiceRequestException e)
{
    // Get the response from the exception.
    batchResponse = e.Response;

    if (batchResponse.IsBatchResponse)
    {
        // Parse the batchResponse.BatchHeaders.
    }

    foreach (QueryOperationResponse response in batchResponse)
    {
        if (response.Error != null)
        {
            Console.WriteLine("An error occurred.");
            Console.WriteLine(response.Error.Message);
        }
    }
}


.NET Framework

Supported in: 4, 3.5 SP1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2016 Microsoft