.netasp.net-web-apiodataasp.net-web-api-odata

Use OData $select to cherry pick fields from related object


I'm using WebAPI 2.2 with OData V4.

It is possible for me to use $filter=RelatedObj/PropertyName eq 'Some Value' to filter a list of entities based on a related object property value.

However, when I try to use the same syntax with $select:

$select=Id,Name,RelatedObj/PropertyName

results in exception:

"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",

Can this be solved?


Solution

  • You can do it using $expand and nested $select query option

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

    See the ODATA documentation