odata4 $filter 返回错误结果

odata4 $filter is returning false results

我正在使用 DataTables odata 插件来增强我的 html 表。搜索时,我发送的过滤器 属性 看起来像这样:

$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'

但是,在结果中,我得到了与具有 indexof 函数的第一个过滤器匹配的所有内容。例如:

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

其中 Manager.Id 甚至不接近我使用过滤器请求的那个。 我的问题是,之前的过滤器是否覆盖了最后一个,或者我以错误的方式请求它?

首先,请注意,您提供的示例是对名为 Manager_Id 的 属性 进行过滤,但示例结果中没有出现这样的 属性。您是要过滤 Manager/Id 吗?

您看到的结果是由于 operator precedence. The and operator has higher precedence than or. You can override precedence by using parentheses to group 子字符串匹配在一起。

最后,您可以使用 contains 函数代替 indexof/eq 组合来简化子字符串匹配。

这是重写后的过滤器(为了便于阅读插入了换行符):

$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'