I need to update the property values of some rows in an Azure Storage Table using Azure.Data.Tables
. I use a query to pull them, update the property with the new value, and add them to a batch for resubmittal. I'd like to print this to the console (at least during testing). The problem I'm having is you have to use a different method to get the value depending on the data type of the value you are retrieving. That is, if the column type is string, Entity.GetString("column_name")
but if its an integer, its Entity.GetInt32("column_name")
or maybe Entity.GetInt64("column_name")
. I don't know this at compile time, so how do I structure the code to handle the different data types?
// Do a maxPerPage of 100 so we have nice even batches
AsyncPageable<TableEntity> queryResultsFilter = tableClient.QueryAsync<TableEntity>(filter: queryFilter, maxPerPage: 100);
// Iterate the "Pageable" to access all queried entities.
await foreach (Page<TableEntity> page in queryResultsFilter.AsPages())
{
p++;
// Initialize a batch
List<TableTransactionAction> Batch = new List<TableTransactionAction>();
foreach (TableEntity qEntityRow in page.Values)
{
i++;
// Add a merge operation to the batch.
// We specify an ETag value of ETag.All to indicate that this merge should be unconditional.
TableEntity mergeEntity = new TableEntity(qEntityRow.PartitionKey, qEntityRow.RowKey) { { column, newValue }, };
Batch.Add(new TableTransactionAction(TableTransactionActionType.UpdateMerge, mergeEntity, ETag.All));
Console.WriteLine($"({p}:{i}) {qEntityRow.GetString("date")} - {qEntityRow.GetString("tagName")}: ");
}
await tableClient.SubmitTransactionAsync(Batch).ConfigureAwait(false);
}
TableEntity is a dictionary. The vanilla this[key], will get you part of the way there:
Now if you are trying to have a strongly typed table entity then you need a class that inherits from
ITableEntity
defining the column properties you want.Example:
Then a quick retooling of your code above would look something like: