How to: Execute Data Service Queries (WCF Data Services)

WCF Data Services enables you to query a data service from a .NET Framework-based client application by using the generated client data service classes. You can execute queries by using one of these methods:

  • Executing a LINQ query against the named DataServiceQuery that you obtain from the DataServiceContext that the Add Data Service Reference tool generates.

  • Implicitly, by enumerating over the named DataServiceQuery that you obtain from the DataServiceContext that the Add Data Service Reference tool generates.

  • Explicitly, by calling the Execute method on the DataServiceQuery, or the BeginExecute method for asynchronous execution.

For more information, see Querying the Data Service (WCF Data Services).

The example in this topic uses the Northwind sample data service and autogenerated client data service classes. This service and the client data classes are created when you complete the WCF Data Services quickstart.

Example

The following example shows how to define and execute a LINQ query that returns all Customers against the Northwind data service.

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

try
{
    // Define a LINQ query that returns all customers.
    var allCustomers = from cust in context.Customers
                       select cust;

    // Enumerate over the query obtained from the context.
    foreach (Customer customer in allCustomers)
    {
        Console.WriteLine("Customer Name: {0}", customer.CompanyName);
    }
}
catch (DataServiceQueryException ex)
{
    throw new ApplicationException(
        "An error occurred during query execution.", ex);
}

The following example shows how to use the context that the Add Data Service Reference tool generates to implicitly execute a query that returns all Customers against the Northwind data service. The URI of the requested Customers entity set is determined automatically by the context. The query is executed implicitly when the enumeration occurs.

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

// Define a new query for Customers.
DataServiceQuery<Customer> query = context.Customers;

try
{
    // Enumerate over the query result, which is executed implicitly.
    foreach (Customer customer in query)
    {
        Console.WriteLine("Customer Name: {0}", customer.CompanyName);
    }
}
catch (DataServiceQueryException ex)
{
    throw new ApplicationException(
        "An error occurred during query execution.", ex);
}

The following example shows how to use the DataServiceContext to explicitly execute a query that returns all Customers against the Northwind data service.

// Define a request URI that returns Customers.
Uri customersUri = new Uri("Customers", UriKind.Relative);

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

try
{
    // Enumerate over the query result.
    foreach (Customer customer in context.Execute<Customer>(customersUri))
    {
        Console.WriteLine("Customer Name: {0}", customer.CompanyName);
    }
}
catch (DataServiceQueryException ex)
{
    throw new ApplicationException(
        "An error occurred during query execution.", ex);
}

See Also

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft