如何在Azure表存储中使用RowKey或时间戳检索最新记录 [英] How to retrieve latest record using RowKey or Timestamp in Azure Table storage

查看:140
本文介绍了如何在Azure表存储中使用RowKey或时间戳检索最新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

棘手的部分是RowKeystring,其值类似于Mon Nov 14 12:26:42 2016

Tricky part is RowKey is string which is having value like Mon Nov 14 12:26:42 2016

我尝试使用Timestamp之类的查询

var lowerlimit = DateTime.UtcNow; // its should be nearer to table timestamp data.
            TableQuery<TemperatureEntity> query2 = new TableQuery<TemperatureEntity>().Where(TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.GreaterThanOrEqual,lowerlimit));
            var test = table.ExecuteQuery(query2);

MyEntity.cs

  public class MyEntity : TableEntity
    {
        public MyEntity(string partitionKey, string rowKey)
        {
            this.PartitionKey = partitionKey;
            this.RowKey = rowKey;
        }

        public MyEntity() { }

        public Int64 DevideId { get; set; }

        public string RowKey { get; set; }
    }

//下面的查询给出完整的数据 Program.cs

//below query gives full data Program.cs

// Retrieve the storage account from the connection string.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
                CloudConfigurationManager.GetSetting("StorageConnectionString"));

            // Create the table client.
            CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

            // Create the CloudTable object that represents the "TemperatureData" table.
            CloudTable table = tableClient.GetTableReference("TemperatureData");

            // retrive data
            TableQuery<TemperatureEntity> query = new TableQuery<TemperatureEntity>();
            var data = table.ExecuteQuery(query);

推荐答案

Azure Table Service不支持Order By功能,因此,使用当前设置的唯一选项是下载所有实体,并按时间顺序对它们进行反向排序客户端.当表中的实体数量变多时,这显然不是最佳解决方案.

Azure Table Service doesn't support Order By functionality, thus with the current setup only option for you is to download all entities and sort them reverse chrnologically on the client side. This obviously is not an optimal solution when the number of entities in the table become large.

其他选项(这将要求您重新设计应用程序)将以相反的刻度转换日期/时间值:

Other option (which would require you to redesign the application) would be to convert the date/time value in reverse ticks:

var rowKey = (DateTime.MaxValue.Ticks - DateTimeValueForRowKey.Ticks).ToString("d19")

这将确保将最新条目添加到表的顶部而不是表的底部.要获取最新条目,只需从表中获取第1个实体.

This will ensure that the latest entries are added to the top of the table instead of at the bottom of the table. To fetch the latest entry, you would just have to take 1st entity from the table.

这篇关于如何在Azure表存储中使用RowKey或时间戳检索最新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆