OData v2通过$expand展开自定义属性数组时$filter正确URI写法
OData V2 嵌套自定义属性筛选问题
接口返回结构参考
调用OData v2 GetEntitySet接口获取的JSON返回结构如下:
{ d: { __count: "3215", result : [ { City : 'NewYork', Country : 'USA', CustomAttributeData : { results : [ { Name: "custom1", Value: "20220707" }, { Name: "custom2", Value: "20220710" }, { Name: "custom3", Value: "20220713" } ] } }, { City : 'Rome', Country : 'ITALY', CustomAttributeData : { results : [ { Name: "custom1", Value: "20220702" }, { Name: "custom2", Value: "20220710" }, { Name: "custom3", Value: "20220710" } ] } }, { City : 'Tokyo', Country : 'JAPAN', CustomAttributeData : { results : [ { Name: "custom1", Value: "20220710" }, { Name: "custom2", Value: "20220711" }, { Name: "custom3", Value: "20220710" } ] } } ] } }
需求说明
筛选出CustomAttributeData中custom2属性值等于20220710的所有城市(即排除东京,返回纽约、罗马的结果)。
原有错误写法
https://xxxxxx?$expand=CustomAttributeData$filter=CustomAttributeData/Value eq '20220710'
该写法会错误返回东京数据。
错误原因
- 系统查询参数缺少分隔符:
$expand和$filter是独立的OData查询选项,中间必须用&连接 - 筛选逻辑缺失:仅判断自定义属性值等于20220710,没有限定匹配的属性名是
custom2,东京的custom1、custom3值均为20220710,因此被误命中。
正确请求URI
OData V2中筛选集合类型的嵌套属性,需要使用any运算符遍历集合项,同时匹配属性名和属性值,正确写法:
https://xxxxxx?$expand=CustomAttributeData&$filter=CustomAttributeData/any(attr: attr/Name eq 'custom2' and attr/Value eq '20220710')
逻辑说明
any运算符会遍历每个城市下CustomAttributeData集合的所有自定义属性项- 遍历过程中只有同时满足「属性名为custom2」「属性值为20220710」的项才算匹配
- 仅当城市存在至少一个符合上述规则的属性项时才会被返回,自动排除custom2值为20220711的东京数据。
内容的提问来源于stack exchange,提问作者Sebastiano Marchesini
相关产品推荐
相关产品推荐

