How to: Add Query Options to a Data Service Query (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. The easiest to do this is to compose a Language Integrated Query (LINQ) query expression that includes the desired query options. You can also call a series of LINQ query methods to compose an equivalent query. Finally, you can use the AddQueryOption method to add query options to a query. In each of these cases, the URI that is generated by the client includes the requested entity set with the selected query options applied. For more information, see Querying the Data Service.

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.

The following example shows how to compose a LINQ query expression that returns only orders with a freight cost of more than $30 and that orders the results by the ship date in descending order.

[!CODE [Astoria Northwind Client#AddQueryOptionsLinq](../CodeSnippet/VS_Snippets_Misc/astoria northwind client#addqueryoptionslinq)]

The following example shows how to compose a LINQ query by using LINQ query methods that is equivalent to the previous query.

[!CODE [Astoria Northwind Client#AddQueryOptionsLinqExpression](../CodeSnippet/VS_Snippets_Misc/astoria northwind client#addqueryoptionslinqexpression)]

The following example shows how to use to the AddQueryOption method to create a DataServiceQuery<TElement> that is equivalent to the previous examples.

[!CODE [Astoria Northwind Client#AddQueryOptions](../CodeSnippet/VS_Snippets_Misc/astoria northwind client#addqueryoptions)]

The following example shows how to use the $orderby query option to both filter and order returned Orders objects by the Freight property.

[!CODE [Astoria Northwind Client#OrderWithFilter](../CodeSnippet/VS_Snippets_Misc/astoria northwind client#orderwithfilter)]

Querying the Data Service
How to: Project Query Results