在存储帐户中以 azure table 读取一行

Read a row in azure table in a storage account

我有一个 table 存储,其中包含 n 个 table(Table 1 到 Table n)。我需要对这些 tables 执行迭代并 从每个 tables.

的一行中读取时间戳值

这是我的代码:

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

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

请尝试以下代码:

我正在使用这个 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();
        }
    }
}

如果您对此有更多问题,请告诉我。