简体   繁体   English

带有天蓝色表存储的分页(升序)

[英]paging (ascendingly ordered) with azure table storage

This works fine to page over data in steps of at most 1000 items: 这样可以很好地在最多1000个项目的步骤中分页浏览数据:

var q1 =
  (from book in table.CreateQuery<DynamicTableEntity>()
   where book.PartitionKey == "TestPartition"
   select book).AsTableQuery();

TableContinuationToken continuationToken = null;
do
{
    var counter = 0;
    var queryResult = q1.ExecuteSegmented(continuationToken);

    foreach (var entity in queryResult)
    {
    Console.WriteLine(entity.Timestamp + " " + ++counter);
    }

    continuationToken = queryResult.ContinuationToken;
    Console.WriteLine("####" + counter);
} while (continuationToken != null);

What I would really like to do is start with the oldest items first. 我真正想做的是首先从最早的物品开始。 In other words page over items ordered ascendingly by entity.Timestamp. 换句话说,按entity.Timestamp升序排列的项目上的页面。 This query does not work: 此查询不起作用:

    var q1 =
  (from book in table.CreateQuery<DynamicTableEntity>()
   where book.PartitionKey == "TestPartition"
   select book).OrderBy(x => x.Timestamp).AsTableQuery();

    TableContinuationToken continuationToken = null;
    do
    {
        var counter = 0;
        var queryResult = q1.ExecuteSegmented(continuationToken);

        foreach (var entity in queryResult)
        {
        Console.WriteLine(entity.Timestamp + " " + ++counter);
        }

        continuationToken = queryResult.ContinuationToken;
        Console.WriteLine("####" + counter);
    } while (continuationToken != null);

As OrderBy is not supported. 由于不支持OrderBy。 Is there anything I can do to achieve this? 我能做些什么来实现这一目标? Thanks. 谢谢。

PS: PS:

This may help. 可能会有所帮助。 However it retrieves the newest items first, whereas I want to retrieve the oldest first. 但是,它首先检索最新的项目,而我想首先检索最旧的项目。

The TableQuery does not support order by. TableQuery不支持order by。 You can find the supported operators: Supported operators 您可以找到支持的运算符: 支持的运算符

This link and the quoted white paper helped me to solve this one. 链接和引用的白皮书帮助我解决了这一问题。

Step 1: 第1步:

Use ticks as rowkey whilst creating the entities like so: 在创建实体的过程中,使用刻度作为行键:

var rowKey = (DateTime.UtcNow.Ticks - DateTime.MinValue.Ticks).ToString();
var entity = new DynamicTableEntity("TestEventPartition", rowKey);

To order the entities ascendingly during paged retrieval, use the 'TableContinuationToken approach' involving the rowkey ticks: 要在分页检索期间升序排列实体,请使用涉及行键刻度的“ TableContinuationToken方法”:

var rowKeyToUse = string.Format("{0:D19}", DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks);

var q1 =
  (from entity in table.CreateQuery<DynamicTableEntity>()
   where entity.PartitionKey == "TestPartition"
   && string.Compare(entity.RowKey, rowKeyToUse, StringComparison.Ordinal) > 0
   select entity).AsTableQuery();

TableContinuationToken continuationToken = null;
do
{
    var counter = 0;
    var queryResult = q1.ExecuteSegmented(continuationToken);

    foreach (var entity in queryResult)
    {
    Console.WriteLine("[" + entity.RowKey + "]"
         + ++counter
         );      
    }

    continuationToken = queryResult.ContinuationToken;
    Console.WriteLine("####" + counter);
} while (continuationToken != null);

Hope this helps someone else. 希望这对其他人有帮助。 Any improvement suggestion/criticism welcome. 任何改进建议/批评表示欢迎。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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