Azure / azure-cosmos-table-dotnet

.NET SDK for Azure Cosmos Table API
14 stars 5 forks source link

TableRequestOptions.TableQueryMaxItemCount is not being honored. #54

Closed AliRafiee closed 3 years ago

AliRafiee commented 4 years ago

Hi,

I'm using Microsoft.Azure.Cosmos.Table to query a storage account table. I'm trying to get a max of 100 items per query.

This is my query:

            var query = new TableQuery()
                .Where(TableQuery.GenerateFilterConditionForDate(
                    "Timestamp",
                    QueryComparisons.LessThanOrEqual,
                    DateTimeOffset.UtcNow - TimeSpan.FromDays(numDays)));

            var requestOptions = new TableRequestOptions()
            {
                TableQueryMaxItemCount = 100
            };

            var count = 0;
            TableContinuationToken token = null;
            do
            {
                var result = await table.ExecuteQuerySegmentedAsync(query, token, requestOptions, operationContext: null);
                Console.WriteLine($"Got {result.Results.Count} items");
                count += result.Results.Count;
                token = result.ContinuationToken;
            } while (token != null);

            Console.WriteLine($"Total = {count}");

What I get instead is: Got 530 items Got 0 items Got 200 items Got 67 items Got 0 items Got 100 items Got 300 items Got 300 items Got 0 items Got 199 items Got 304 items Total = 2000

I'm not sure what I'm doing wrong here.

PaulCheng commented 3 years ago

For storage table end point, Use TableQuery.Take() method. var query = new TableQuery().Where(...).Take(100);

BadgerCode commented 2 years ago

For anyone else who finds this, set the TakeCount property on your query to limit this. TableRequestOptions.TableQueryMaxItemCount does nothing.


var query = new TableQuery<TableRowModel>()
    .Where(TableQuery.GenerateFilterCondition("RuntimeStatus", QueryComparisons.Equal, "Running")); // Example where clause
query.TakeCount = 10; // Limit query to 10 items
var results = _instancesTable.ExecuteQuery(query).ToList();