Skip to content

Grouping Example

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

Source documents can be grouped together by specifying properties to group by. Each group of source documents will then be aggregated into a separate aggregate result element.

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

var query = container.CreateAggregateDocumentQuery<Sale>(queryOptions)
    .GroupBy(x => x.StoreId)
    .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