简体   繁体   中英

Determining the data type of a value from an Azure Storage Table entity

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:

var colvalue = qEntityRow["ColumnNameYourAfter"];

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:

public record VideoEntity : ITableEntity
{
    public string VideoPageTitle { get; set; }
    public int StartingPageNumber { get; set; }
    public int StopAtPageNumber { get; set; }
    public string GeoLocation { get; set; }
    public string TrendCategory { get; set; }
    public string MostViewedRelatedCategory { get; set; }
};

Then a quick retooling of your code above would look something like:

    // Do a maxPerPage of 100 so we have nice even batches
    AsyncPageable<VideoEntity> queryResultsFilter = tableClient.QueryAsync<VideoEntity>(filter: queryFilter, maxPerPage: 100);
    // Iterate the "Pageable" to access all queried entities.
    await foreach (Page<VideoEntity> page in queryResultsFilter.AsPages())
    {
        p++;
        // Initialize a batch
        List<TableTransactionAction> Batch = new List<TableTransactionAction>();
        foreach (var video 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.
            var mergeEntity = new VideoEntity(video.PartitionKey, video.RowKey) 
            { 
                { column, newValue } // <- Your update...
            };
            Batch.Add(new TableTransactionAction(TableTransactionActionType.UpdateMerge, mergeEntity, ETag.All));
            Console.WriteLine($"({p}:{i}) {video.VideoPageTitle} - Page {video.StartingPageNumber} - {video.StopAtPageNumber}");
        }
        var results = await tableClient.SubmitTransactionAsync(Batch);
        // Do any additional processing with the results or use _ for discard.
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM