user989988
user989988

Reputation: 3746

Read a row in azure table in a storage account

I have a table storage that contains n number of tables (Table 1 to Table n). I need to perform an iteration over these tables and read Timestamp value from one row in each of these tables.

Here is my code:

        var storageAccount = CloudStorageAccount.Parse(storageConnectionString);
        var tableClient = storageAccount.CreateCloudTableClient();
        var tables = tableClient.ListTables();
        foreach (var table in tables)
        {
            // read timestamp value in a table row
            var query = new TableQuery<PersonEntity>();            
            foreach (var message in table.ExecuteQuery(query))
            {
                Console.WriteLine(message.Timestamp);
                timestampFromTable = message.Timestamp;
                break;
            }       
        }

Is there a better way to read row from a table and avoid nested for loop?

Upvotes: 0

Views: 599

Answers (1)

Ivan Glasenberg
Ivan Glasenberg

Reputation: 30035

Please try the code below:

I'm using this nuget package: Microsoft.Azure.Cosmos.Table, version 1.0.7.

The code as below and please feel free to change it to meet your need:

using Microsoft.Azure.Cosmos.Table;
using System;
using System.Collections.Generic;
using System.Linq;

namespace TableStorageTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var storageConnectionString = "xxxx";
            var storageAccount = CloudStorageAccount.Parse(storageConnectionString);
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            //define which column which be fetched
            List<string> mylist = new List<string>() { "Timestamp" };

            var tables = tableClient.ListTables();

            foreach (var table in tables)
            {
                // read timestamp value in a table row
                var query = new TableQuery<CustomerEntity>()
                    .Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "ivan4")).Select(mylist);

                var result = table.ExecuteQuery(query).ToList();

                if (result.Count >0)
                {
                    var r = result[0].Timestamp;

                    Console.WriteLine(r);
                }

            }

            Console.WriteLine("**completed**");
            Console.ReadLine();
        }
    }
}

Please let me know if you have more issues about this.

Upvotes: 1

Related Questions