21

Is there any way I can get only count of the data in response payload without any value array?

I am using ODataV4.0 with Webapi 2.2. Currently it returns all the values and count when I query something like: http://odata/People?$count=true

I just need something like "@odata.count":1, "value":[] or without "value".

Is the only way to have function for this job?

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
ManojAnavatti
  • 604
  • 1
  • 7
  • 18

2 Answers2

49

Set the $top to zero and $count to true.

For example: http://services.odata.org/V4/Northwind/Northwind.svc/Customers?$count=true&$top=0

returns the count but no results

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

Count is calculated after applying the $filter, but without factoring in $top and $skip.

For example: http://services.odata.org/V4/Northwind/Northwind.svc/Customers?$count=true&$top=0&$filter=Country%20eq%20%27Germany%27

informs you that there are 11 results where the Country is 'Germany', but without returning any records in the response.

shaedrich
  • 5,457
  • 3
  • 26
  • 42
Lawrence McAlpin
  • 2,745
  • 20
  • 24
  • 4
    Hi, i just tried this and if i add $top=0 to the query parameters i don't get the @odata.count. Not sure if they have changed something. Is there another way to get only OData properties? – D. Gencheva Jun 28 '18 at 06:52
  • I see that this answer correctly describes what is actually happening on the sample endpoint, but, based on the docs, I do not understand why that is the case. I have asked about this in [a related question](https://stackoverflow.com/questions/61319024/why-does-this-odata-query-with-count-true-return-an-object). – F-H Apr 20 '20 at 09:23
  • @F-H this is because the northwind example service is a .net (wcf data service) implementation, so you need to refer to the .net documentation: https://learn.microsoft.com/en-us/odata/concepts/queryoptions-overview#count but ah, its not fully compliant with that one either because the service is very old and no longer maintained. – Chris Schaller Aug 11 '20 at 23:24
  • I am new to oData, however it looks like that if $count=true always returns all values as well, this could be a security vulnerability. Basically you handover a banch of api endpoints that can hammer your servers and could translate in skyrocketing costs. – Panos Dec 03 '22 at 11:13
15

You can also append $count as a path element to just get a raw count, E.G.,

https://services.odata.org/V4/Northwind/Northwind.svc/Customers/$count

This will also work with filters, etc, applied: https://services.odata.org/V4/Northwind/Northwind.svc/Customers/$count?$filter=Country%20eq%20%27Germany%27

For a count of Customers in Germany.

absmiths
  • 1,144
  • 1
  • 12
  • 21
  • 1
    Is this latter use of `$count` to be found anywhere in the documentation? If appending arguments such as `$filter` to a `$count` route is considered standard-conformant, this could save me [a lot of hassle](https://stackoverflow.com/questions/61322851/how-can-i-parse-odataqueryoptions-from-a-string). – F-H Apr 20 '20 at 18:24
  • @F-H : http://docs.oasis-open.org/odata/odata/v4.01/odata-v4.01-part2-url-conventions.html#sec_AddressingtheCountofaCollection – Chris Schaller Aug 11 '20 at 09:43
  • what if you wanted to return a unique count per each or more than one country? for a count of customers per each country? – topwik Feb 23 '22 at 18:23