5

I am using cosmos client and C# to delete items from a collection. In the process I would like to check the number of records that are in the collection, I have the data part of it and the query but I am stuck at what to use since I will be only be receiving an int not a stream of data.

For deleting data I am using FeedIterator since we get a stream of data. FeedIterator queryResultSetIterator = this.container.GetItemQueryIterator(queryDefinition);

List orders = new List();

I delete the records by looping through the list

What should I be using in place of FeedIterator since the output is going to be the number of records in the collection?

var sqlQueryText1 = "SELECT value count(1) from c where c.tenantId = 
'5d484526d76e9653e6226aa2'";
QueryDefinition queryDefinition1 = new QueryDefinition(sqlQueryText1);

This is the article I used to help with the console app: https://learn.microsoft.com/en-us/azure/cosmos-db/sql-api-get-started

Jai
  • 155
  • 2
  • 9

1 Answers1

7

There are two ways of getting the count.. You can use GetItemQueryIterator:

var query = new QueryDefinition("SELECT value count(1) FROM c WHERE c.tenantId = @type");
        query.WithParameter("@type", '5d484526d76e9653e6226aa2');
        var container = client.GetContainer("DatabaseName", "CollectionName");
        var iterator = container.GetItemQueryIterator<int>(query);
        var count = 0;
        while (iterator.HasMoreResults)
        {
            var currentResultSet = await iterator.ReadNextAsync();
            foreach (var res in currentResultSet)
            {
                count += res;
            }
        }
        Console.WriterLine($"The first count is: {count}");

Or you can use the GetItemLinqQueryable:

var count = container.GetItemLinqQueryable<Infobit>(true)
            .Count(item => item.tenantId.Equals('5d484526d76e9653e6226aa2'));

        Console.WriteLine($"The second count is: {count}");
Zaphod
  • 1,412
  • 2
  • 13
  • 27
  • Which one would be a better solution in terms of RU's? Also, how the processing happens for both of the methods? – Balanjaneyulu K Jan 14 '20 at 09:00
  • @BalanjaneyuluK I'm sorry, but I don't actually know which sollution is best regarding RU's. I prefer the first option, but can't give you a decise answer as to why. – Zaphod Jan 15 '20 at 08:55