Reputation: 2997
I'm rounding down the current UTC date to the nearest minute and converting it to ticks to save the result as a PartitionKey
in Azure Table storage. I'd like to run a range query to get specific partition key ranges in code. I'm using C#, but clearly the below LINQ operations are not supported.
var tableQuery = cloudTable.CreateQuery<Log>().Where(x => long.Parse(x.PartitionKey) <= ticks);
throws
System.NotSupportedException: 'The expression (Parse([10007].PartitionKey) <= 637224147600000000) is not supported.'
I have also tried String.Compare()
, but it doesn't work either. So, how can I write a filtered query in C# to get records less than or equal to given ticks? Note that the partition key is chosen this way so that records can naturally be sorted in descending order of time.
Upvotes: 0
Views: 2596
Reputation: 4573
Below is the example, I used to fetch data. Try below query with your condition. I think it should work.
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();
}
Upvotes: 2