Why does this OData query with $count=true return an object?

时光毁灭记忆、已成空白 提交于 2020-05-31 06:18:13

问题


I am trying to figure out how to correctly support returning the number of items in a (filtered) data set in my OData API.

My understanding is that adding the $count=true argument to the query string should allow for this.

Now, based on the example from the tutorial in the official docs, adding that parameter should cause the web API to return just an integer number:

The request below returns the total number of people in the collection.

GET serviceRoot/People?$count=true

Response Payload

20

On the other hand, this accepted and quite upvoted1 answer indicates that a query with $count=true will actually return an object, one of whose properties holds said integer number. It provides an exemplary query on a sample endpoint:

https://services.odata.org/V4/Northwind/Northwind.svc/Customers?$count=true&$top=0&$filter=Country eq 'Germany'

Indeed, the actual result from that endpoint is the complex object

{
  "@odata.context": "https://services.odata.org/V4/Northwind/Northwind.svc/$metadata#Customers",
  "@odata.count": 11,
  "value": []
}

instead of the expected result of a single integer number

11

Why is this? Am I misunderstanding the documentation?

1: The answer had 25 upvotes at the time of writing.

来源:https://stackoverflow.com/questions/61319024/why-does-this-odata-query-with-count-true-return-an-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!