If you need to add filtering logic to your API, here’s a quick guide with the most common operators:
Example:
/Order?$filter=CreatedOn gt 2006-12-30T23:59:59.99Z
The Equals operator (or โeqโ) evaluates to true if the left operand is equal to the right operand, otherwise if evaluates to false.
Ex.: /Order?$filter=OrderStatusId eq 5
The URL above will return all the Orders with OrderStatusId = 5 (Approved)
Other operators include:
- Not Equals (or โneโ)
- Greater Than (or โgtโ)
- Greater Than or Equal (or โgeโ)
- Less Than (or โltโ)
- Less Than (or โleโ)
- Logical And (or โandโ)
- Logical Or (or โorโ)
- Logical Negation Operator (or โnotโ)
The following examples illustrate the use and semantics of each of the logical operators:
Product?$filter=Name eq ‘Milk’ (Requests all products with a Name equal to ‘Milk’)
Product?$filter=Name ne ‘Milk’ (Requests all products with a Name not equal to ‘Milk’)
Product?$filter=Name gt ‘Milk’ (Requests all products with a Name greater than ‘Milk’)
Product?$filter=Name ge ‘Milk’ (Requests all products with a Name greater than or equal to ‘Milk’)
Product?$filter=Name lt ‘Milk’ (Requests all products with a Name less than ‘Milk’)
Product?$filter=Name le ‘Milk’ (Requests all products with a Name less than or equal to ‘Milk’)
Product?$filter=Name eq ‘Milk’ and Price lt ‘2.55M’ (Requests all products with the Name ‘Milk’ that also have a Price less than 2.55)
Product?$filter=Name eq ‘Milk’ or Price lt ‘2.55M’ (Requests all products that either have the Name ‘Milk’ or have a Price less than 2.55)
Product?$filter=not endswith(Name, ‘ilk’) (Requests all products that do not have a Name that ends with ‘ilk’)
For more reference and examples, refer to the official OData documentation page on URL conventions:
https://www.odata.org/documentation/odata-version-3-0/url-conventions/