Use OData $select to cherry pick fields from related object
Asked Answered
L

2

11

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?

Licketysplit answered 16/6, 2015 at 13:13 Comment(0)
P
19

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

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

See the ODATA documentation

Protrude answered 27/1, 2016 at 17:15 Comment(1)
This doesn't work for (some?) properties. I get the error: "Invalid $select properties." I don't think the above syntax is supported for Microsoft Graph API (neither beta nor v1): learn.microsoft.com/en-us/graph/…Eleazar
B
0

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
Brisance answered 18/6, 2015 at 19:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.