OData provide functionality of processing data directly inside the server on the data provided by ORM or the service itself, and recive a minimal version of data, which is useful to the client.
OData functionality can be achived using $ signed http query string options
The Query Options are used to define the process that should be made over source data...
Microsoft Implemented a limited version of OData, and involving it's ORM, and Custom Models, this limitation increase, due to that, all of these operation may not work at all times..
The Main Operation are below:
To filter data using OData you need to use $filter... and assign the actual filter you want
So, a simple filter consists of "$filter=" part and a conditional expression in front of it which will appear inside a query string part of a URL
For example, to filter flights that they have capacity of greater than 0, we will use followig url:
http://<flightServiceUrl>/flights?$filter=Capacity gt 0
and it's encoded version can look like this:
http://<flightServiceUrl>/flights?%24filter=Capacity+gt+0
Note that the operand are case sensitive.
to build a filter you can use these operators:
Operator | Description | Example |
---|---|---|
Logical Operators | ||
Eq | Equal | /Suppliers?$filter=Address/City eq 'Redmond' |
Ne | Not equal | /Suppliers?$filter=Address/City ne 'London' |
Gt | Greater than | /Products?$filter=Price gt 20 |
Ge | Greater than or equal | /Products?$filter=Price ge 10 |
Lt | Less than | /Products?$filter=Price lt 20 |
Le | Less than or equal | /Products?$filter=Price le 100 |
And | Logical and | /Products?$filter=Price le 200 and Price gt 3.5 |
Or | Logical or | /Products?$filter=Price le 3.5 or Price gt 200 |
Not | Logical negation | /Products?$filter=not endswith(Description,'milk') |
Arithmetic Operators | ||
Add | Addition | /Products?$filter=Price add 5 gt 10 |
Sub | Subtraction | /Products?$filter=Price sub 5 gt 10 |
Mul | Multiplication | /Products?$filter=Price mul 2 gt 2000 |
Div | Division | /Products?$filter=Price div 2 gt 4 |
Mod | Modulo | /Products?$filter=Price mod 2 eq 0 |
Grouping Operators | ||
( ) | Precedence grouping | /Products?$filter=(Price sub 5) gt 10 |
Also following function can be of help in a more complex query:
Using the $orderby query option you can sort data, and using comma, for non-distinct values, you can define secondary sort options too
For example if you want to sort flight by date and flight number, you can do the following:
http://<flightServiceUrl>/flights?$orderby=Date,FlightNumber desc
By defult sort operation will return data in a ascending order, for in above case, first we returned flight by Date in ascending order, and for flights with same date, we returned them in a descending order
Using $top query option, we can limit the result of query, from the top of the list
And using $skip we can ignore some of the result from the top
For example:
To get 5 top item in the list:
http://<flightServiceUrl>/flights?$top=5
To ignore 10 first item in the list:
http://<flightServiceUrl>/flights?$skip=5
to get the 3rd page, when each page size is 10
http://<flightServiceUrl>/flights?$top=30&$skip=20
Depend on the source ORM, or data processor, you may be forced to include $orderby query option too, otherwise it may generate errors.
using $select query option, we can project the property that are included in the results.
for example, if you want to know only the Flight Number, and Airline of a flight service, you will use following query:
http://<flightServiceUrl>/flights?$select=FlightNumber,Airline