在存储帐户中的天蓝色表中读取一行

时间:2020-06-15 19:59:10

标签: c# azure-table-storage

我有一个表存储,其中包含n个表(表1至表n)。我需要对这些表进行迭代,并从每个表的一行中读取时间戳记值

这是我的代码:

        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;
            }       
        }

是否有更好的方法从表中读取行并避免嵌套for循环?

1 个答案:

答案 0 :(得分:1)

请尝试以下代码:

我正在使用以下nuget软件包:Microsoft.Azure.Cosmos.Table, version 1.0.7

以下代码,请随时对其进行更改以满足您的需求:

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();
        }
    }
}

如果您还有其他问题,请告诉我。