Search code examples
asp.net-web-apiodata

odata4 $filter is returning false results


I am using DataTables odata plugin to power up my html tables. When searching I am sending filter property that looks like this :

$filter=    
indexof(tolower(ClientAlias/Name), 'wee') gt -1 or indexof(tolower(Product/Name), 'wee') gt -1 or indexof
(tolower(User/UserName), 'wee') gt -1 or indexof(tolower(Manager/FullName), 'wee') gt -1 and Status ne
 webapi.Models.ContractStatus'Suspended' and Manager_Id eq '120'

However, in the results i get absolutely everything that matches the first filters with the indexof function. For example :

{
ClientAlias:Object{Name="weentertain"}
Manager:
Object { Id="55"}
}

Where the Manager.Id is not even close to the one that I am requesting with the Filter. My question is, do the previous filters overwrite the last one, or I am requesting it in a wrong way?


Solution

  • First, note that the example you gave is filtering on a property named Manager_Id, but no such property appears in your sample results. Did you mean to filter on Manager/Id?

    The results you are seeing are due to operator precedence. The and operator has higher precedence than or. You can override precedence by using parentheses to group the substring matches together.

    Finally, you can simplify the substring matches by using the contains function in place of the indexof/eq combination.

    Here's the rewritten filter (with line breaks inserted for readability):

    $filter=
      (contains(tolower(ClientAlias/Name), 'wee') or
       contains(tolower(Product/Name), 'wee') or
       contains(tolower(User/UserName), 'wee') or 
       contains(tolower(Manager/FullName), 'wee')) and
      Status ne webapi.Models.ContractStatus'Suspended' and
      Manager/Id eq '120'