Skip to content

Collection Counts

mattl91 edited this page Jan 26, 2026 · 10 revisions

Harmony Core Logo

Collection Counts

OData services have an optional feature that allows operations that return a collection of entities to instead only return the number of entities in a particular result set. This is conceptually similar to the SQL SELECT COUNT operation.

Remember, though, that underlying data is still held within ISAM or relative files, so it's still necessary to run the full query in order to determine the result count. But at least all of that work happens in the back end environment and does not propagate all the way through the web service. Because of this, it can still be a useful feature.

To obtain the count of a result set, a consumer appends /$count to the end of a query URL. But in order to be supported, you must first enable the capability in your REST API. This is done by enabling the "Enable collection count endpoints" option on the OData tab of the CLI Tool.

What Changes

Enabling collection counts does not cause additional files to be generated, but causes additional content to be generated into various files, specifically:

  • A new statement builder.Count() is added to the OData configuration section of the Startup class in the Services project.
  • The additional capability is documented in the API Documentation if enabled.
  • Additional tests are added to the postman file if enabled.

Testing the New Functionality

In Visual Studio, press F5 (Start Debugging) to start the self-hosting application. Once again you should see the console window appear, and messages confirming that your service is running. Open your web browser and navigate to the "all customers" endpoint:

https://localhost:8086/odata/v1/Customers

You should see a JSON response that includes the full record for all 38 customers in the sample customer data file. Now add /$count to the end of the URL and re-execute the query:

https://localhost:8086/odata/v1/Customers/$count

The response should now be simply:

38

Another way to use this feature is to use a slightly different syntax to instruct OData to include the result count with the result data. For example, the following URL:

https://localhost:8086/odata/v1/Customers?$count=true

This request causes all customer entities to be returned, but you will notice that an additional piece of metadata @odata.count is included in the response:

{
    "@odata.context": "https://harmonycoreapp.azurewebsites.net/odata/v1/$metadata#Customers",
    "@odata.count": 38,
    "value": [
      {
        "@odata.etag": "W/\"YmluYXJ5J0FDQUFBQUFBNHhsU3FRPT0n\"",
        "CustomerNumber": 1,

Note that this functionality can be combined with other OData query expressions, for example:

https://localhost:8086/odata/v1/Customers?$filter=State+eq+'CA'&$count=true

This returns a subset of the collection of customers, based on the specified filter criteria, and also includes the @odata.count metadata.

{
  "@odata.context": "https://harmonycoreapp.azurewebsites.net/odata/v1/$metadata#Customers",
  "@odata.count": 13,
  "value": [
    {
      "@odata.etag": "W/\"YmluYXJ5J0FDQUFBQUFBNHhsU3FRPT0n\"",
      "CustomerNumber": 1,

Note: The $count functionality should work with all endpoints and queries that return a collection, but currently it only works with the full collection endpoint. We think this is because of a limitation in the underlying OData library that we use, and we'll be investigating further.

Clone this wiki locally