我试图通过嵌套集合进行排序,筛选计数,但无法在OData中找到如何进行排序。
我将使用公共OData API (来自Microsoft)作为演示来解释我的意思,不需要授权。
https://services.odata.org/TripPinRESTierService/
所以我们有People
在那里
https://services.odata.org/TripPinRESTierService/People
这些People
有Trips
https://services.odata.org/TripPinRESTierService/People?$expand=Trips
如果我想通过Trips
通过Name
过滤,这很容易
https://services.odata.org/TripPinRESTierService/People?$expand=Trips($filter=Name eq 'Trip in Beijing')
如果我想按People
计数订购Trip
,也很容易
https://services.odata.org/TripPinRESTierService/People?$expand=Trips($count=true)&$orderby=Trips/$count desc
但我无法通过过滤后的次数来订购(比如$expand=Trips($filter=Name eq 'Trip in Beijing')
)
https://services.odata.org/TripPinRESTierService/People?$expand=Trips($count=true;$filter=Name eq 'Trip in Beijing')&$orderby=Trips/$count desc
类似于&$orderby=Trips($filter=Name eq 'Trip in Beijing')/$count desc
或&$orderby=Trips/$count($filter=Name eq 'Trip in Beijing') desc
的东西不起作用(给出服务器错误"Could not find a property named 'Name' on type 'Trippin.Person'."
)。
此URL的版本以查看最低要求的数据
https://services.odata.org/TripPinRESTierService/People?$expand=Trips($top=0;$count=true;$filter=Name eq 'Trip in Beijing')&$orderby=Trips/$count desc&$select=Trips
更新:在我的项目中使用Microsoft.AspNetCore.OData
版本7.5.7,它依赖于Microsoft.OData.Core
版本7.7.3 (它没有显式安装)。如果微软将在其API中升级OData版本,它可能也会起作用。
发布于 2021-12-11 19:44:39
更新只支持基于这个公关的Microsoft.OData.Core版本>= 7.9.4。
您应该能够在$orderby中重复您在$expand中具有的相同的筛选条件,以获得您要寻找的内容。
/odata/People?$expand=Trips($count=true;$filter=Name eq 'Paris')&$orderby=Trips/$count($filter=Name eq 'Paris') desc
{
"@odata.context": "https://localhost:5001/odata/$metadata#People(Trips())",
"value": [
{
"Id": "d31713ca-236b-470f-bb9e-f5bd6fac3d8e",
"Name": "Aaron",
"Trips@odata.count": 2,
"Trips": [
{
"Id": "741656bf-f5e6-4f18-855f-ca1b3c0eb478",
"Name": "Paris"
},
{
"Id": "741656bf-f5e6-4f18-855f-ca1b3c0eb478",
"Name": "Paris"
}
]
},
{
"Id": "34c0ac10-1c0d-4a29-89ad-386a3fea893b",
"Name": "Bob",
"Trips@odata.count": 1,
"Trips": [
{
"Id": "1b35a96d-ca98-438a-b63c-1478ae0661cb",
"Name": "Paris"
}
]
},
{
"Id": "192dd197-6f25-4842-bb9f-207b5df221c2",
"Name": "Jeff",
"Trips@odata.count": 0,
"Trips": []
}
]
}
比较而言,这里是没有$filter的相同数据集。
/odata/People?$expand=Trips($count=true)&$orderby=Trips/$count desc
{
"@odata.context": "https://localhost:5001/odata/$metadata#People(Trips())",
"value": [
{
"Id": "192dd197-6f25-4842-bb9f-207b5df221c2",
"Name": "Jeff",
"Trips@odata.count": 3,
"Trips": [
{
"Id": "4537521c-27a6-4fa4-8bb5-fd94937abd56",
"Name": "Cleveland"
},
{
"Id": "40431cba-0d8f-470a-9647-bdd99009737a",
"Name": "Cleveland"
},
{
"Id": "1269c3e3-b5ff-4a73-8201-5388d00ea744",
"Name": "Cleveland"
}
]
},
{
"Id": "d31713ca-236b-470f-bb9e-f5bd6fac3d8e",
"Name": "Aaron",
"Trips@odata.count": 2,
"Trips": [
{
"Id": "741656bf-f5e6-4f18-855f-ca1b3c0eb478",
"Name": "Paris"
},
{
"Id": "741656bf-f5e6-4f18-855f-ca1b3c0eb478",
"Name": "Paris"
}
]
},
{
"Id": "34c0ac10-1c0d-4a29-89ad-386a3fea893b",
"Name": "Bob",
"Trips@odata.count": 1,
"Trips": [
{
"Id": "1b35a96d-ca98-438a-b63c-1478ae0661cb",
"Name": "Paris"
}
]
}
]
}
https://stackoverflow.com/questions/70229635
复制相似问题