Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 440
ManojAnavatti Avatar asked May 08 '15 11:05

ManojAnavatti


People also ask

How do I filter OData query?

You can use filter expressions in OData requests to filter and return only those results that match the expressions specified. You do this by adding the $filter system query option to the end of the OData request.

What is $Top and $skip?

Query options $top and $skip are used to restrict the amount of data retrieved from the back-end system. Client-side paging is possible by using this query option $top=n query option will retrieve the top n records from the OData service feed/collection.

How do I query OData service?

You can find details on filter specification in the OData spec filter options section. Examples: All products with a Name equal to 'Milk': http://host/service/Products?$filter=Name eq 'Milk' All products with a Name not equal to 'Milk' : http://host/service/Products?$filter=Name ne 'Milk'

What is $select in OData?

The $select option specifies a subset of properties to include in the response body. For example, to get only the name and price of each product, use the following query: Console Copy. GET http://localhost/odata/Products?$select=Price,Name.


2 Answers

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.

like image 96
Lawrence McAlpin Avatar answered Oct 08 '22 01:10

Lawrence McAlpin


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.

like image 35
absmiths Avatar answered Oct 08 '22 00:10

absmiths