Use OData $select to cherry pick fields from related object

12.5k views Asked by At

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?

2

There are 2 answers

1
avitenberg On BEST ANSWER

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

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

See the ODATA documentation

0
mitch On

If you want to perform a $select on an item under a navigation property, you need to first $expand the navigation property.

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