LINQ enables you to compose queries against a collection of objects that implements IEnumerable<T>. Both the Add Service Reference dialog box in Visual Studio and the DataSvcUtil.exe tool are used to generate a representation of an OData service as an entity container class that inherits from DataServiceContext, as well as objects that represent the entities returned in feeds. These tools also generate properties on the entity container class for the collections that are exposed as feeds by the service. Each of these properties of the class that encapsulates the data service return a DataServiceQuery<TElement>. Because the DataServiceQuery<TElement> class implements the IQueryable<T> interface defined by LINQ, you can compose a LINQ query against feeds exposed by the data service, which are translated by the client library into a query request URI that is sent to the data service on execution.
The set of queries expressible in the LINQ syntax is broader than those enabled in the URI syntax that is used by OData data services. A NotSupportedException is raised when the query cannot be mapped to a URI in the target data service. For more information, see the Unsupported LINQ Methods in this topic.
The following example is a LINQ query that returns Orders that have a freight cost of more than $30 and sorts the results by the shipping date, starting with the latest ship date:
Dim selectedOrders = From o In context.Orders _
Where (o.Freight > 30) _
Order By o.ShippedDate Descending _
This LINQ query is translated into the following query URI that is executed against the Northwind-based quickstart data service:
http://localhost:12345/Northwind.svc/Orders?Orderby=ShippedDate&?filter=Freight gt 30
For more general information about LINQ, see LINQ (Language-Integrated Query).
LINQ enables you to compose queries by using both the language-specific declarative query syntax, shown in the previous example, as well as a set of query methods known as standard query operators. An equivalent query to the previous example can be composed by using only the method-based syntax, as shown the following example:
Dim selectedOrders = context.Orders _
.Where(Function(o) o.Freight.Value > 30) _
The WCF Data Services client is able to translate both kinds of composed queries into a query URI, and you can extend a LINQ query by appending query methods to a query expression. When you compose LINQ queries by appending method syntax to a query expression or a DataServiceQuery<TElement>, the operations are added to the query URI in the order in which methods are called. This is equivalent to calling the AddQueryOption method to add each query option to the query URI.