使用OData $ select来从相关对象中挑选字段

时间:2015-06-16 13:13:02

标签: .net asp.net-web-api odata asp.net-web-api-odata

我正在使用带有OData V4的WebAPI 2.2。

我可以使用$filter=RelatedObj/PropertyName eq 'Some Value'根据相关的对象属性值过滤实体列表。

但是,当我尝试使用与$select相同的语法时:

$select=Id,Name,RelatedObj/PropertyName

导致异常:

"message": "The query specified in the URI is not valid. Found a path with multiple navigation properties or a bad complex property path in a select clause. Please reword your query such that each level of select or expand only contains either TypeSegments or Properties.",
"innererror": {
"message": "Found a path with multiple navigation properties or a bad complex property path in a select clause. Please reword your query such that each level of select or expand only contains either TypeSegments or Properties.",
"type": "Microsoft.OData.Core.ODataException",

这可以解决吗?

2 个答案:

答案 0 :(得分:8)

您可以使用$expand和嵌套$select查询选项

来执行此操作
$select=Id,Name&$expand=RelatedObj($select=PropertyName)

请参阅ODATA documentation

答案 1 :(得分:-2)

如果要对导航属性下的项目执行$select,则需要先$expand导航属性。

EntitySet?$select=Id,Name,RelatedObj/PropertyName&$expand=RelatedObj