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

后端 未结 3 972
甜味超标
甜味超标 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:35

    This is in addition on to @JoseCh.'s answer.

    Here is an extension method which allows you to specify an EntityResolver:

    public static async Task> ExecuteQueryAsync(this CloudTable table, TableQuery query, EntityResolver resolver, Action> onProgress = null, CancellationToken cancelToken = default(CancellationToken))
                where T : ITableEntity, new()
    {
        var items = new List();
        TableContinuationToken token = null;
    
        do
        {
            TableQuerySegment seg = await table.ExecuteQuerySegmentedAsync(query: query, resolver: resolver, token: new TableContinuationToken(), cancellationToken: cancelToken).ConfigureAwait(false);
            token = seg.ContinuationToken;
            items.AddRange(seg);
            onProgress?.Invoke(items);
         }
         while (token != null && !cancelToken.IsCancellationRequested);
             return items;
         }
    }
    

    It can be used if you only want to return the result set of a single column in storage:

    // maps to a column name in storage
    string propertyName = nameof(example.Category);
    
    // Define the query, and select only the Category property.
    var projectionQuery = new TableQuery().Select(new string[] { propertyName });
    
    // Define an entity resolver to work with the entity after retrieval.
    EntityResolver resolver = (pk, rk, ts, props, etag) => props.ContainsKey(propertyName) ? props[propertyName].StringValue : null;
    
    var categories = (await someTable.ExecuteQueryAsync(query: projectionQuery, resolver: resolver).ConfigureAwait(false)).ToList()
    

提交回复
热议问题