Export (0) Print
Expand All

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 (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.

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.

' Create the DataServiceContext using the service URI. 
Dim context = New NorthwindEntities(svcUri)

' Define a query for orders with a Freight value greater than 30 
' and that is ordered by the ship date, descending. 
Dim selectedOrders = From o In context.Orders _
        Where (o.Freight > 30) _
        Order By o.ShippedDate Descending _
        Select o

Try 
    ' Enumerate over the results of the query. 
    For Each order As Order In selectedOrders
        Console.WriteLine("Order ID: {0} - Ship Date: {1} - Freight: {2}", _
                order.OrderID, order.ShippedDate, order.Freight)
    Next 
Catch ex As DataServiceQueryException
    Throw New ApplicationException( _
            "An error occurred during query execution.", ex)
End Try

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

' Create the DataServiceContext using the service URI. 
Dim context = New NorthwindEntities(svcUri)

' Define a query for orders with a Freight value greater than 30 
' and that is ordered by the ship date, descending. 
Dim selectedOrders = context.Orders _
                     .Where(Function(o) o.Freight.Value > 30) _
                     .OrderByDescending(Function(o) o.ShippedDate)

Try 
    ' Enumerate over the results of the query. 
    For Each order As Order In selectedOrders

        Console.WriteLine("Order ID: {0} - Ship Date: {1} - Freight: {2}", _
                order.OrderID, order.ShippedDate, order.Freight)
    Next 
Catch ex As DataServiceQueryException
    Throw New ApplicationException( _
        "An error occurred during query execution.", ex)
End Try

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

' Create the DataServiceContext using the service URI. 
Dim context = New NorthwindEntities(svcUri)

' Define a query for orders with a Freight value greater than 30 
' and that is ordered by the ship date, descending. 
Dim selectedOrders As DataServiceQuery(Of Order) = context.Orders _
.AddQueryOption("$filter", "Freight gt 30") _
.AddQueryOption("$orderby", "OrderID desc")

Try 
    ' Enumerate over the results of the query. 
    For Each order As Order In selectedOrders
        Console.WriteLine("Order ID: {0} - Ship Date: {1} - Freight: {2}", _
                order.OrderID, order.ShippedDate, order.Freight)
    Next 
Catch ex As DataServiceQueryException
    Throw New ApplicationException( _
            "An error occurred during query execution.", ex)
End Try

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

' Create the DataServiceContext using the service URI. 
Dim context = New NorthwindEntities(svcUri)

' Define a query for orders with a Freight value greater than 30 
' that also orders the result by the Freight value, descending. 
Dim selectedOrders As DataServiceQuery(Of Order) = _
context.Orders.AddQueryOption("$orderby", "Freight gt 30 desc")

Try 
    ' Enumerate over the results of the query. 
    For Each order As Order In selectedOrders
        Console.WriteLine("Order ID: {0} - Freight: {1}", _
                order.OrderID, order.Freight)
    Next 
Catch ex As DataServiceQueryException
    Throw New ApplicationException( _
            "An error occurred during query execution.", ex)
End Try
Show:
© 2014 Microsoft