# Querying

OData supports various kinds of query options for querying data. This section will help you go through the common scenarios for these query options.

# $filter

The $filter system query option allows clients to filter a collection of resources that are addressed by a request URL. The expression specified with $filter is evaluated for each resource in the collection, and only items where the expression evaluates to true are included in the response.

There are several kinds of basic predicates and built-in functions for $filter, including logical operators and arithmetic operators. For more detailed information, please refer to the OData protocol (opens new window) document. The request below uses $filter to get people with FirstName "Scott".

$filter can also be used as a path segment, and used multiple times to sequentially add filter parameters.

# $orderby

The $orderby system query option allows clients to request resources in either ascending order using asc or descending order using desc. If asc or desc not specified, then the resources will be ordered in ascending order. The request below orders People on property LastName in descending order.

# $top and $skip

The $top system query option requests the number of items in the queried collection to be included in the result. The $skip query option requests the number of items in the queried collection that are to be skipped and not included in the result. The request below returns the first two people of the People entity set.

The request below returns people starting with the 2nd person of the entity set People

# $count

The $count system query option allows clients to request a count of the matching resources included with the resources in the response. The request below returns the total number of people in the collection.

# $expand

The $expand system query option specifies the related resources to be included in line with retrieved resources. The request below returns people with navigation property Passengers of a Flight

# $select

The $select system query option allows the clients to requests a limited set of properties for each entity or complex type. The request below returns origin and destination of all flights:

The $search system query option restricts the result to include only those entities matching the specified search expression. The definition of what it means to match is dependent upon the implementation. The request below gets all airports that have 'fo' or 'lh' in their code.

# $index

The value of the $index system query option is the zero-based ordinal position where an item is to be inserted. This option is supported on entity sets that can be numerically indexed.

The ordinal positions of items within the collection greater than or equal to the inserted position are increased by one. A negative ordinal number indexes from the end of the collection, with -1 representing an insert as the last item in the collection.

# $compute

The $compute system query option allows clients to define computed properties that can be used in a $select or within a $filter or $orderby expression. $compute uses the same common expression syntax as $filter. Computed properties appear as dynamic properties in the result. Some entity set drivers may support including computed properties in the result but without supporting their use in $orderby or $filter statements.

# $any and $all

OData defines two lambda operators any and all that evaluate a Boolean expression on a collection. They can work on either collection properties or collections of entities.

The request below returns the People that have Pets with property 'Name' ending with 'The Dog'.

# $each

The $each path segment enables actions and odata operations such as deletes and updates to be run on sequences of entities server-side. To filter the sequence the $filter path segment must be used.

Members of a collection can be updated by submitting a PATCH request to the URL constructed by appending /$each to the resource path of the collection. The additional path segment expresses that the request body describes an update to each member of the collection, not an update to the collection itself.

Members of a collection can be deleted by submitting a DELETE request to the URL constructed by appending /$each to the resource path of the collection. The additional path segment expresses that the collection itself is not deleted.

DELETE http://localhost/odata/People/$filter(@bar)/$each?@bar=Color eq 'beige-brown'
1

A bound operation with a single-valued binding parameter can be applied to each member of a collection by appending the path segment $each to the resource path of the collection, followed by a forward slash and the namespace- or alias-qualified name of the bound operation.

GET http://localhost/odata/People/$each/SampleModel.MostRecentOrder()
1