OData aggregate query with Count

2019-09-15 04:46发布

问题:

In odata 5.3.1 the aggregate query was not working so that I wrote my custom facet class to get aggregate count of items. http://localhost/odata/Document$apply=groupby((Category),%20aggregate(Document/$count%20as%20Total))

But in Odata 5.9.1 the above query is failing as because it has already aggregate function included. So for my custom method it is not working.

GithHub aggregate function

I want data something like this :

  "value": [
{ "name" : "doc1", "version" 2: , "total": 5 },
{ "name" : "doc2", "version" 1: , "total": 8 },
{ "name" : "doc2", "version" 2: , "total": 2 },

]

So how the query should be? Here "version" is a string property.

回答1:

Found the right query for this operation. Add same class property to your model class. Let there is model class called Document & add a property

public Document Documents {get;set;} 

Then for Asp.net Odata 5.9.1 use key word countdistinct

 http://localhost/odata/Document?$apply=groupby((Category), aggregate(Documents with countdistinct as Total))

So it's returning exact Total count.