查询Azure表存储-使用值的静态数组进行比较



我需要修改和现有的Azure表存储查询,假设I是一个整数查询,检索最新报告:

string rowCompare = String.Format(CommonDefs.inverseTimeStampRowKeyFormat, DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks);
var result = (from er in this.serviceContext.EntityReportsTable
                           where er.PartitionKey.Equals(i.ToString(), StringComparison.OrdinalIgnoreCase) && er.RowKey.CompareTo(rowCompare) > 0
                           select er).Take(1)).FirstOrDefault();

我需要对其进行修改,以检索几个已知实体的最新报告,将单个整数I替换为整数数组,如int[]{1,6,10}。

除了对数组中的每个参数按顺序运行现有的查询之外,有没有一种方法可以在一个查询中执行?像Sql中的IN子句吗?

您可以使用Azure存储客户端库的最新版本,这是您任务的完整伪代码:

var rowCompare = String.Format("{0}", DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks);
var items = new []{"1", "6", "10"};
var filters =
    items.Select(key => TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, key)).ToArray();
var combine =
    filters.Length > 0
        ? filters[0]
        : null;
for (var k = 0; k < filters.Length; k++)
    combine = TableQuery.CombineFilters(combine, TableOperators.Or, filters[k]);
var final = TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.GreaterThan, rowCompare);
if (!string.IsNullOrEmpty(combine))
    final = TableQuery.CombineFilters(final, TableOperators.And, combine);
var query = new TableQuery<EntityReport>().Where(final);
var client = CloudStorageAccount.DevelopmentStorageAccount.CreateCloudTableClient();
var table = client.GetTableReference("EntityReports");
var result = table.ExecuteQuery(query);

Azure表存储不支持IN clause like SQL。但是,您可以并行启动查询并比较结果,而不是按顺序执行查询。例如,看看下面的伪代码:

        List<Task<T>> tasks = new List<Task<T>>();
        foreach (var i in integerArray)
        {
            tasks.Add(Task.Factory.StartNew<T>(() => {
                string rowCompare = String.Format(CommonDefs.inverseTimeStampRowKeyFormat, DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks);
                var result = (from er in this.serviceContext.EntityReportsTable
                                           where er.PartitionKey.Equals(i.ToString(), StringComparison.OrdinalIgnoreCase) && er.RowKey.CompareTo(rowCompare) > 0
                                           select er).Take(1)).FirstOrDefault();
            }));
        }
        Task.WaitAll(tasks.ToArray());
        foreach (var task in tasks)
        {
            var queryResult = task.Result;
            //Work on the query result
        }

相关内容

  • 没有找到相关文章

最新更新