Skip to content

Simple Example

Chris Dahlberg edited this page Mar 19, 2019 · 1 revision

Aggregate queries are created by calling the CreateAggregateDocumentQuery<T> extension method on CosmosContainer. The simplest usage only requires the Aggregate method to be called before executing the query, which will aggregate all documents in the collection and return a single result element.

CosmosContainer container = {{your_target_container}};
            
var queryOptions = new AggregateQueryOptions { PartitionKey = "13" };

var query = container.CreateAggregateDocumentQuery<Sale>(queryOptions)
    .Aggregate((aggregate, current) => new Sale
        {
            StoreId = aggregate.StoreId,
            Amount = aggregate.Amount + current.Amount,
        })
    .AsDocumentQuery();

var results = new List<Sale>();

while (query.HasMoreResults)
{
    var resultsPage = await query.ExecuteNextAsync(cancellationToken).ConfigureAwait(false);
                
    results.AddRange(resultsPage);
}

Clone this wiki locally