【问题标题】:Use OData $select to cherry pick fields from related object使用 OData $select 从相关对象中挑选字段
【发布时间】:2015-09-01 07:58:59
【问题描述】:

我正在使用带有 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",

这可以解决吗?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      您可以使用$expand 和嵌套的$select 查询选项来做到这一点

      $select=Id,Name&$expand=RelatedObj($select=PropertyName)
      

      ODATA documentation

      【讨论】:

      • 这不适用于(某些?)属性。我收到错误消息:“$select 属性无效。”我认为 Microsoft Graph API(无论是 beta 版还是 v1)都不支持上述语法:docs.microsoft.com/en-us/graph/…
      猜你喜欢
      • 2012-10-21
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      相关资源
      最近更新 更多