Multiple filter conditions Azure table storage Multiple filter conditions Azure table storage azure azure

Multiple filter conditions Azure table storage


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);


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 testvar startDate = DateTime.Parse("01/02/2016 12:00:00 AM"); var endDate = DateTime.Parse("02/02/2016 12:00:00 AM");// Get the cloud tablevar cloudTable = GetCloudTable();// Create a query: in this example I use the DynamicTableEntity classvar query = cloudTable.CreateQuery<DynamicTableEntity>()        .Where(d => d.PartitionKey == "partition1"               && d.Timestamp >= startDate && d.Timestamp <= endDate);// Execute the queryvar 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.


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

I was wondering the same thing. I wrote an extension to the TableQuery class which works fine.

It's an easy change which makes me wonder if we are going about querying with multiple filters incorrectly.

public static class TableQueryExtensions {    public static TableQuery<TElement> AndWhere<TElement>(this TableQuery<TElement> @this, string filter)    {        @this.FilterString = TableQuery.CombineFilters(@this.FilterString, TableOperators.And, filter);        return @this;    }    public static TableQuery<TElement> OrWhere<TElement>(this TableQuery<TElement> @this, string filter)    {        @this.FilterString = TableQuery.CombineFilters(@this.FilterString, TableOperators.Or, filter);        return @this;    }    public static TableQuery<TElement> NotWhere<TElement>(this TableQuery<TElement> @this, string filter)    {        @this.FilterString = TableQuery.CombineFilters(@this.FilterString, TableOperators.Not, filter);        return @this;    }}