I am using Cosmos DB Table API to manage my data(Using SQL API is not an option). I have used "Creation DateTime Ticks" as the "PartitionKey". The idea is to retrieve data in every half an hour. To get the new data in half an hour range, I wrote a method, which is like this - (Update - Based on Gaurav's suggestion, I have updated the code).
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");
` I was getting an exception - "Method 'Visit' in type 'QueryTokenVisitor' from assembly 'Microsoft.Azure.Cosmos.Table, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' does not have an implementation.". I tried to use LINQ query too. But I could not make it work. The other thing which I tried , was , TableQuery.GenerateFilterCondition(). That works for a specific "PartitionKey" and "RowKey" but not for the range of the "PartitionKey". How can I use Cosmos DB Table API to get result for the given DateTime range? I am new to Azure Table API.