Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting only the count of odata expand property

I have a OData service with WebAPI OData 6.0.0, and it supports queries like this:

/Customers?$expand=Projects($count=true)

This will return a list of Customers with all the Projects underneath each Customer with the count of those projects in addition for each.

What I now would like to have is a query that will get the list of customers, and for each customer ONLY the count of its projects, not the list of projects.

Is there a way to create such a query?

I tried

/Customers?$select=Name,Projects/$count

but this is not working.

like image 773
Malyngo Avatar asked Nov 24 '16 09:11

Malyngo


People also ask

How do you expand OData?

OData expand functionality can be used to query related data. For example, to get the Course data for each Enrollment entity, include ?$ expand=course at the end of the request path: This tutorial uses Postman to test the web API.

What is ODataquery?

The Open Data Protocol (OData) is a data access protocol built on core protocols like HTTP and commonly accepted methodologies like REST for the web. There are various kinds of libraries and tools can be used to consume OData services.

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.


1 Answers

You can kind of accomplish this by using $expand with $top option. It will output Projects as an empty collection.

/Customers?$select=Name&$expand=Projects($count=true;$top=0)
like image 137
Mika Tähtinen Avatar answered Oct 12 '22 04:10

Mika Tähtinen