How to construct IQueryable query using Linq when I just need count without reading all documents in Document-Db database?

Viewed 3474

I need the number of documents stored in a collection in my Azure Cosmos Db database. How can I get the count using LINQ query on the IQueryable object?

docDbClient.CreateDocumentQuery<TResult>().Count()

If I do above, I am unable to follow it up with .AsDocumentQuery() method.

3 Answers

Another way to acheive that is:

using Microsoft.Azure.Cosmos.Linq; // required for CountAsync()
//...
cosmosClient
    .GetContainer(databaseName, containerName)
    .GetItemLinqQueryable<MyRecord>(true)
    .Where(r => r.Name == "John")
    .Where(r => r.Status == MyRecordStatus.Approved)
    .CountAsync()
Related