我正在使用 Cosmos DB Table API 来管理我的数据(使用 SQL API 不是一个选项)。我使用“创建日期时间刻度”作为“分区键”。这个想法是每半小时检索一次数据。为了得到半小时范围内的新数据,我写了一个方法,是这样的——(更新——基于 Gaurav 的建议,我已经更新了代码)。
public async Task<List<TEntity>> GetEntityWithDateTimePartitionKeyAsync<TEntity>(long startDateTimeTicks , long endDateTimeTicks, string tableName) where TEntity : TableEntity, new()
{
var results = new List<TEntity>();
if (endDateTimeTicks > startDateTimeTicks)
{
var table = await this.GetTableAsync(tableName, true).ConfigureAwait(false);
var filterA = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.GreaterThanOrEqual, startDateTimeTicks.ToString());
var filterB = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.LessThan, endDateTimeTicks.ToString());
var combinedFilter = TableQuery.CombineFilters(filterA, "AND", filterB);
var query = new TableQuery<TEntity>().Where(combinedFilter);
try
{
results = table.ExecuteQuery<TEntity>(query).ToList();
}
catch(Exception ex)
{
}
}
return results;
}
// Sample Data -
public class TestItem: TableEntity
{
}
//Create the instances and then save them to Cosmos Db.
var testItem1 = new TestItem { PartitionKey ="637671350058032346",RowKey= "Mumbai", ETag="*" };
var testItem2 = new TestItem {PartitionKey = "637671350058033346", RowKey="Delhi" , ETag="*"};
var testItem3 = new TestItem { PartitionKey ="637671350058034346", RowKey="Chennai" , ETag="*"};
var testItem4 = new TestItem { PartitionKey ="637671350058035346", RowKey="Hyderabad" , ETag="*"}
//Calling the method -
var entityList = await GetEntityWithDateTimePartitionKeyAsync<TestItem>(637671350058030000 , 637671350058036000, "TestTable");
` 我遇到了一个异常 - “来自程序集 'Microsoft.Azure.Cosmos.Table, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 的 'QueryTokenVisitor' 类型中的方法 'Visit' 没有实现。”。我也尝试使用 LINQ 查询。但我无法让它工作。我尝试的另一件事是TableQuery.GenerateFilterCondition()。这适用于特定的“ PartitionKey ”和“ RowKey ”,但不适用于“PartitionKey”的范围。如何使用 Cosmos DB Table API 获取给定 DateTime 范围的结果?我是 Azure 表 API 的新手。