使用 azure table 存储进行分页(升序排列)

paging (ascendingly ordered) with azure table storage

这可以很好地按最多 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);

我真正想做的是先从最旧的项目开始。换句话说,翻阅按 entity.Timestamp 升序排列的项目。此查询不起作用:

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

因为不支持 OrderBy。我能做些什么来实现这一目标?谢谢

PS:

This 可能会有帮助。但是它首先检索最新的项目,而我想首先检索最旧的项目。

TableQuery 不支持排序依据。您可以找到支持的运算符:Supported operators

这个 link 和引用的白皮书帮助我解决了这个问题。

第 1 步:

像这样创建实体时使用刻度作为行键:

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

要在分页检索期间对实体进行升序排序,请使用涉及行键刻度的 'TableContinuationToken approach':

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

希望这对其他人有帮助。欢迎任何改进suggestion/criticism。