ManojAnavatti
ManojAnavatti

Reputation: 644

How to get only Odata.Count without value

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?

Upvotes: 24

Views: 45809

Answers (3)

Rasmus Christensen
Rasmus Christensen

Reputation: 8541

You can use aggregate as in ?$filter=statecode eq 0&$apply=aggregate($count as Count)

This will return only the count :)

Upvotes: 0

absmiths
absmiths

Reputation: 1174

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.

Upvotes: 17

Lawrence McAlpin
Lawrence McAlpin

Reputation: 2785

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.

Upvotes: 53

Related Questions