代码之家  ›  专栏  ›  技术社区  ›  user246392

Azure表:如何编写范围查询以筛选分区键

  •  0
  • user246392  · 技术社区  · 6 年前

    我正在将当前的UTC日期舍入到最接近的分钟,并将其转换为刻度以将结果保存为 PartitionKey 在Azure表存储中。我想运行一个范围查询来获取代码中特定的分区键范围。我正在使用C#,但显然不支持下面的LINQ操作。

    var tableQuery = cloudTable.CreateQuery<Log>().Where(x => long.Parse(x.PartitionKey) <= ticks); 
    

    投掷

    System.NotSupportedException: 'The expression (Parse([10007].PartitionKey) <= 637224147600000000) is not supported.'
    

    我也试过 String.Compare()

    1 回复  |  直到 6 年前
        1
  •  2
  •   Pankaj Rawat    6 年前

    下面是我用来获取数据的示例。请根据您的情况尝试下面的查询。我觉得应该管用。

    public async Task<Advertisement> GetAdvertisementBy(string id, string user)
        {
            List<Advertisement> list = new List<Advertisement>();
            // Initialize the continuation token to null to start from the beginning of the table.
            TableContinuationToken continuationToken = null;
            var filter1 = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, user);
            var filter3 = TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, id);
            var combine = TableQuery.CombineFilters(filter1, TableOperators.And, filter3);
            TableQuery<Advertisement> query = new TableQuery<Advertisement>().Where(combine);
            do
            {
                // Retrieve a segment (up to 1,000 entities).
                TableQuerySegment<Advertisement> tableQueryResult = await base.Table.ExecuteQuerySegmentedAsync(query, continuationToken);
                // Assign the new continuation token to tell the service where to
                // continue on the next iteration (or null if it has reached the end).
                continuationToken = tableQueryResult.ContinuationToken;
                list.AddRange(tableQueryResult.Results);
                // Loop until a null continuation token is received, indicating the end of the table.
            } while (continuationToken != null);
    
            return list.FirstOrDefault();
        }