I am trying to get a list of all entities inside an azure table.
Any idea of how I would write this query?
A possibly more efficient way of lazily retrieving items from the table is this:
public IEnumerable GetAll(string tableName) where T : class
{
var table = this.GetCloudTable(tableName);
TableContinuationToken token = null;
do
{
var q = new TableQuery();
var queryResult = Task.Run(() => table.ExecuteQuerySegmentedAsync(q, token)).GetAwaiter().GetResult();
foreach (var item in queryResult.Results)
{
yield return item;
}
token = queryResult.ContinuationToken;
} while (token != null);
}
If the caller is looping through the result of GetAll and find what they were looking for, they could just break the loop, and the GetAll method would stop retrieving next items. This might be more efficient, though this wouldn't make much difference if you really had to retrieve all items.
If using C# 8.0 you can yield inside async methods:
public async Task> GetAll(string tableName) where T : class
{
var table = this.GetCloudTable(tableName);
TableContinuationToken token = null;
do
{
var q = new TableQuery();
var queryResult = await table.ExecuteQuerySegmentedAsync(q, token);
foreach (var item in queryResult.Results)
{
yield return item;
}
token = queryResult.ContinuationToken;
} while (token != null);
}