How to execute an Azure table storage query async? client version 4.0.1

后端 未结 3 949
甜味超标
甜味超标 2021-01-30 16:07

Want to execute queries Async on Azure Storage Client Version 4.0.1

There is NO method ExecuteQueryAsync()..

I am missing something? Should we continue to use th

3条回答
  •  爱一瞬间的悲伤
    2021-01-30 16:26

    When table query contains take clause specified solution will return more items than requested by query. Small change of while expression will solve that problem.

    public static async Task> ExecuteQueryAsync(this CloudTable table, TableQuery query, CancellationToken ct = default(CancellationToken), Action> onProgress = null) where T : ITableEntity, new()
    {
        var runningQuery = new TableQuery()
        {
            FilterString = query.FilterString,
            SelectColumns = query.SelectColumns
        };
    
        var items = new List();
        TableContinuationToken token = null;
    
        do
        {
            runningQuery.TakeCount = query.TakeCount - items.Count;
    
            TableQuerySegment seg = await table.ExecuteQuerySegmentedAsync(runningQuery, token);
            token = seg.ContinuationToken;
            items.AddRange(seg);
            if (onProgress != null) onProgress(items);
    
        } while (token != null && !ct.IsCancellationRequested && (query.TakeCount == null || items.Count < query.TakeCount.Value));
    
        return items;
    }
    

    EDITED: Thanks to a suggestion from PaulG, corrected the issue with result count when query contains take clause and ExecuteQuerySegmentedAsync returns items in several passes.

提交回复
热议问题