I am using a synchronous pattern to count items held in CosmosDB, specifying a predicate and using GetItemLinqQueryable
This works nicely, counting all ReportRecord items with a specific field value:
var count = _cosmosClient.GetContainer(DATABASE_ID, CONTAINER_ID)
.GetItemLinqQueryable<ReportRecord>(true)
.Count(r => r.Name.Equals("Smith"));
However, if I try to check a boolean is true in the predicate it doesn't seem to work, i.e. all records are counted but I only want those where Answer is true. Maybe its a syntax issue in my Linq, or my fundamental understanding is wrong.
var count = _cosmosClient.GetContainer(DATABASE_ID, CONTAINER_ID)
.GetItemLinqQueryable<ReportRecord>(true)
.Count(r => r.Answer);
or
var count = _cosmosClient.GetContainer(DATABASE_ID, CONTAINER_ID)
.GetItemLinqQueryable<ReportRecord>(true)
.Count(r => r.Answer == true);
For reference, the ReportRecord looks like this:
public class ReportRecord
{
public string Name { get; set; }
public bool Answer { get; set; }
}
If I switch to the asyn pattern below, then I can query successfully using the following, but I really want to use the sync pattern here for consistency.
List<ReportRecord> results = new List<ReportRecord>();
var container = _cosmosClient.GetContainer(DATABASE_ID, CONTAINER_ID);
var query = new QueryDefinition("SELECT * FROM Reports p WHERE p.answer = true");
using (FeedIterator<ReportRecord> resultSetIterator = container.GetItemQueryIterator<ReportRecord>(query))
{
while (resultSetIterator.HasMoreResults)
{
FeedResponse<ReportRecord> response = await resultSetIterator.ReadNextAsync();
results.AddRange(response);
}
}
return results.Count;