Dot-Net

使用 OData $select 從相關對像中挑選欄位

  • October 28, 2016

我將 WebAPI 2.2 與 OData V4 一起使用。

我可以使用$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",

這可以解決嗎?

您可以使用$expand和嵌套$select查詢選項來做到這一點

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

請參閱ODATA 文件

如果要對$select導航屬性下的項目執行 a,則需要首先$expand導航屬性。

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

引用自:https://stackoverflow.com/questions/30868614