如何执行Azure表存储查询异步?客户端版本4.0.1

Jos*_*Ch. 56 c# azure-storage azure-table-storage

想要在Azure存储客户端版本4.0.1上执行查询异步

没有方法ExecuteQueryAsync()..

我错过了什么?我们应该继续使用ExecuteQuerySegmentedAsync吗?谢谢.

Jos*_*Ch. 77

我最终制作了一个扩展方法来使用ExecuteQuerySegmentedAsync.我不确定这个解决方案是否是最优的,如果有人有任何评论请不要犹豫.

public static async Task<IList<T>> ExecuteQueryAsync<T>(this CloudTable table, TableQuery<T> query, CancellationToken ct = default(CancellationToken), Action<IList<T>> onProgress = null) where T : ITableEntity, new()
    {

        var items = new List<T>();
        TableContinuationToken token = null;

        do
        {

            TableQuerySegment<T> seg = await table.ExecuteQuerySegmentedAsync<T>(query, token);
            token = seg.ContinuationToken;
            items.AddRange(seg);
            if (onProgress != null) onProgress(items);

        } while (token != null && !ct.IsCancellationRequested);

        return items;
    }
Run Code Online (Sandbox Code Playgroud)

  • 您忘记将cancellationToken传入ExecuteQuerySegmentedAsync (3认同)

Dav*_*vor 14

当表查询包含take子句时,指定的解决方案将返回比查询请求更多的项.while表达式的小变化将解决该问题.

public static async Task<IList<T>> ExecuteQueryAsync<T>(this CloudTable table, TableQuery<T> query, CancellationToken ct = default(CancellationToken), Action<IList<T>> onProgress = null) where T : ITableEntity, new()
{
    var runningQuery = new TableQuery<T>()
    {
        FilterString = query.FilterString,
        SelectColumns = query.SelectColumns
    };

    var items = new List<T>();
    TableContinuationToken token = null;

    do
    {
        runningQuery.TakeCount = query.TakeCount - items.Count;

        TableQuerySegment<T> seg = await table.ExecuteQuerySegmentedAsync<T>(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;
}
Run Code Online (Sandbox Code Playgroud)

编辑:感谢PaulG的建议,当查询包含take子句并ExecuteQuerySegmentedAsync在多次传递中返回项时,更正了结果计数的问题.