Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple filter conditions Azure table storage

How can I set multiple filters on a Azure Table Storage?

This is what I've tried:

string partitionFilter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "partition1"); string date1 = TableQuery.GenerateFilterCondition("Date", QueryComparisons.GreaterThanOrEqual, "31-8-2013T14:15:14Z"); string date2 = TableQuery.GenerateFilterCondition("Date", QueryComparisons.LessThanOrEqual, "31-8-2013T14:15:14Z"); string finalFilter = TableQuery.CombineFilters(partitionFilter, TableOperators.And, date1); 

This doesn't work because TableQuery.CombineFilters() only takes 3 parameters. And I need an extra parameter for the 2nd date.

My second try:

string filter = "PartitionKey eq 'partition1' and Date ge datetime'31-8-2013T14:15:14Z' and Date lt datetime'31-8-2013T14:19:10Z'"; TableQuery<CustomEntity> query = new TableQuery<CustomEntity>().Where(filter).Take(5); 

This returns 400 bad request. But if I remove the 'datetime' it runs but returns no results while it should return a few 100 records.

According to this doc from msdn, that is how datetimes should be formatted.

My result should be all records that are between two dates.

How can I make this work?

like image 451
Quoter Avatar asked Aug 31 '13 15:08

Quoter


2 Answers

First "and" your partition filter with one of the date filters, then "and" the intermediate result with the other date filter.

string date1 = TableQuery.GenerateFilterConditionForDate(                    "Date", QueryComparisons.GreaterThanOrEqual,                    DateTimeOffsetVal); string date2 = TableQuery.GenerateFilterConditionForDate(                    "Date", QueryComparisons.LessThanOrEqual,                    DateTimeOffsetVal); string finalFilter = TableQuery.CombineFilters(                         TableQuery.CombineFilters(                             partitionFilter,                             TableOperators.And,                             date1),                         TableOperators.And, date2); 
like image 185
Damith Avatar answered Sep 19 '22 22:09

Damith


I am using Windows Azure Storage 7.0.0 and you can use Linq query so that you don't need to combine filters anymore:

// filter dates for test var startDate = DateTime.Parse("01/02/2016 12:00:00 AM");  var endDate = DateTime.Parse("02/02/2016 12:00:00 AM");  // Get the cloud table var cloudTable = GetCloudTable();  // Create a query: in this example I use the DynamicTableEntity class var query = cloudTable.CreateQuery<DynamicTableEntity>()         .Where(d => d.PartitionKey == "partition1"                && d.Timestamp >= startDate && d.Timestamp <= endDate);  // Execute the query var result = query.ToList(); 

Here is the generated query :

((PartitionKey eq 'partition1') and (Timestamp ge datetime'2016-01-31T11:00:00Z')) and (Timestamp le datetime'2016-02-01T11:00:00Z')

You can notice that:

  • The filters have been combined.
  • The dates have been converted to UTC.
like image 31
Thomas Avatar answered Sep 20 '22 22:09

Thomas