Search code examples
restodata

OData - shortening filter query with multiple conditions on same field


I am trying to perform a rather long OData filter to retrieve a collection of entities that fulfil multiple conditions.

As a straightforward solution, I am currently doing something like this:

$filter=(name eq 'A' or name eq 'B' or name eq 'C')  and (address eq 'addr1' or address eq 'addr2')

From the example, I am targeting 2 fields: 'name' and 'address'. For both of these fields, I am filtering by a (potentially very long) list of eligible values.

While the query presented already works, there's actually a limitation that lies in the character limit on the length of the entire API call (approx. ~8000 characters). If the use case expands, the query length will very likely exceed the character limit.

I have tried something like this to truncate the query a little:

$filter=(name eq 'A' or 'B' or 'C') ...

It doesn't work (syntax error), but I am wondering if there is a better way to structure the query so that the length can be shorter?


Solution

  • You're looking for the in operator

    $filter=name in ('A', 'B', 'C')
    

    https://learn.microsoft.com/en-us/odata/webapi/in-operator