azure-sdk-for-net icon indicating copy to clipboard operation
azure-sdk-for-net copied to clipboard

Write sample for using pageable with continuation tokens

Open christothes opened this issue 2 years ago • 1 comments

Library name

Azure.Data.Tables

Please describe the feature.

example

var query = table.Query<TableEntity>();
    var pages = query.AsPages(pageSizeHint: 10);
    string ct = null;
    IReadOnlyList<TableEntity> results = null;
    var page = pages.First();
    ct = page.ContinuationToken;
    results = page.Values;
    Console.WriteLine("Page:\n=====================");
    foreach (var result in results)
    {
        Console.WriteLine($"{result.PartitionKey}-{result.RowKey}");
    }
    bool hadResults = true;
    while (hadResults)
    {
        hadResults = false;
        Console.WriteLine("press key for next page");
        Console.ReadKey();
        query = table.Query<TableEntity>();
        page = query.AsPages(ct, pageSizeHint: 10).First();
        ct = page.ContinuationToken;
        results = page.Values;
        hadResults = results.Count > 0;
    Console.WriteLine("Page:\n=====================");
        foreach (var result in results)
        {
            Console.WriteLine($"{result.PartitionKey}-{result.RowKey}");
        }
    }

christothes avatar Aug 22 '22 20:08 christothes

Hi @christothes,

Thank you for posting the sample. It just gives more details than the pagination docs.

I'm creating a paged API for data from a storage table and used your sample like this. Maybe it will help someone.

install nuget package "System.Linq.Async" for async linq operations.

public class FooDataServiceV1 : IFooDataServiceV1 {
    private readonly ILogger<AzureResourceServiceV1> _logger;
    private readonly TableClient _tableClient;

    public FooDataServiceV1(ILogger<FooDataServiceV1> logger, TableServiceClient tableServiceClient, IConfiguration configuration)
    {
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
        if (tableServiceClient is null)
        {
            throw new ArgumentNullException(nameof(tableServiceClient));
        }

        if (configuration is null)
        {
            throw new ArgumentNullException(nameof(configuration));
        }

        string tableName = configuration.GetValue<string>("TableName");
        _tableClient = tableServiceClient.GetTableClient(tableName);
    }

    public async Task<Page<FooModel>> GetFooDataByBarFilterV1(string barId, string? cursorValue, int limit = 100)
    {
        if (string.IsNullOrWhiteSpace(barId))
        {
            throw new ArgumentException($"'{nameof(barId)}' cannot be null or whitespace.", nameof(barId));
        }

        _logger.LogDebug($"Looking for data by barId '{barId}'");

        var query = _tableClient.QueryAsync<FooModel>(x => x.PartitionKey == barId);

        var pages = query.AsPages(cursorValue, limit);
        var page = await pages.FirstAsync();

        return page;
    }
}

LockTar avatar Nov 09 '22 10:11 LockTar