编程语言
首页 > 编程语言> > c#-查询Azure表存储-使用静态值数组进行比较

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

作者:互联网

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

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,azure-table-storage,linq,c,net
来源: https://codeday.me/bug/20191029/1961832.html