Querying Overview

Description Article

The WCF Data Services client library enables execution queries against a data service by using common .NET Framework programming patterns.

The query is represented at the Client as a class instance of generic type DataServiceQuery<T>. When executed the client library translates it into an HTTP GET request message.

After execution the library receives the response message and translates it into instances of client data service classes tracked by the DataServiceContext where the query belongs.


The Data Service Query

The DataServiceQuery<T> generic class represents a query. The query returns a collection of zero or more entity type instances (referenced as entitiy set).

A data service query belongs to an existing data service context. 

The Data Service Context

The service context is created as an entity container class when the .NET Client application is generated.

It inherits from the DataServiceContext class and maintains the service URI and metadata information that is required to compose and execute the query.

This class includes the properties that return typed DataServiceQuery instances:
There is one property for each entity set that the data service exposes.

Query Execution

A query is executed when:

  • Results are enumerated implicitly:  when a property on the DataServiceContext that represents an entity set is enumerated (foreach loop);
  • When the query is assigned to a List collection;
  • The Execute or BeginExecute method is explicitly called; or
  • A LINQ query execution operator is called (e.g. First or Single)

Response and Materialization Process

When executed, the query returns an IEnumerable of the requested entity type.
The process of creating instances of objects that represent entities in data service is called Materialization

This query result can then be casted to a QueryOperationResponse object that has the following structure:

Error gets an error thrown by the operation, if any has occurred
Headers contains the collection of HTTP response headers associated with the query response
Query gets the original DataServiceQuery that generated the QueryOperationResponse
StatusCode gets the HTTP response code for the query response
TotalCount gets the total number of entities in the entity set when the “IncludeTotalCount ” method was called on the DataServiceQuery
GetContinuation returns a DataServiceQueryContinuation object that contains the URI of the next page of results

Note:  By default, WCF Data Services only returns data that is explicitly selected by the query URI.