C# 多个筛选条件 Azure 表存储

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18549555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:34:10  来源:igfitidea点击:

Multiple filter conditions Azure table storage

c#azureazure-table-storage

提问by Quoter

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

如何在 Azure 表存储上设置多个筛选器?

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.

这不起作用,因为TableQuery.CombineFilters()只需要 3 个参数。我需要第二个日期的额外参数。

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.

这返回400 bad request. 但是,如果我删除“日期时间”,它会运行但不返回任何结果,而它应该返回几个 100 条记录。

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

根据msdn 的这个文档,这就是日期时间的格式。

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

我的结果应该是两个日期之间的所有记录。

How can I make this work?

我怎样才能使这项工作?

采纳答案by Damith

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

回答by Gaurav Mantri-AIS

Just wanted to add one more answer.

只是想再添加一个答案。

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

The reason code above is failing is because the date/time value must be entered in yyyy-MM-ddTHH:mm:ssZformat. So your query should be:

上面的代码失败的原因是因为必须以yyyy-MM-ddTHH:mm:ssZ格式输入日期/时间值。所以你的查询应该是:

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

回答by LivingOnACloud

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

如何在 Azure 表存储上设置多个筛选器?

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

我想知道同样的事情。我写了一个工作正常的 TableQuery 类的扩展。

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

回答by Thomas

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

我使用的是Windows Azure Storage 7.0.0,您可以使用 Linq 查询,这样您就不再需要组合过滤器了:

// 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')

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

You can notice that:

你可以注意到:

  • The filters have been combined.
  • The dates have been converted to UTC.
  • 过滤器已合并。
  • 日期已转换为 UTC。

回答by Assil

Just handling the case of a new query that does not have a filter already and based on @LivingOnACloud, I would rather write it this way:

只是处理一个没有过滤器并且基于@LivingOnACloud 的新查询的情况,我宁愿这样写:

 public static TableQuery<TElement> AndWhere<TElement>(this TableQuery<TElement> query, string filter)
            where TElement : ITableEntity,new ()
        {
            if (query.FilterString.IsNullOrEmpty())
            {
                query.FilterString =  filter;
            }
            else
            {
                query.FilterString = TableQuery.CombineFilters(query.FilterString, TableOperators.And, filter);
            }
            return query;
        }

And the rest follow the same check, things can go nicer.

其余的都遵循相同的检查,事情会变得更好。