一文的基础上介绍如何自定义 Azure Table storage 的查询过滤条件。如果您还不太清楚 Azure Table storage 的基本用法,请先移步。
功能的方法,因此我们需要自己实现它(还好 TableQuery 的实现支持我们去扩展它)!本文将通过实现 StartsWith 过滤条件说明如何自定义 Azure Table storage 的查询过滤条件。
查询。基本用法是使用查询条件构建一个 TableQuery 类的实例,然后把这个实例作为参数传递给 CloudTable 的ExecuteQuery 方法:
方法 CombineFilters 构建自定义的查询条件。比如我们要查询 PartitionKey 等于 "201607" 并且 RowKey 等于"161148372454"的记录:
函数的返回结果为: "(PartitionKey eq '201607') and (RowKey eq '161148372454')"。函数做参数,或者设置给 query.FilterString 属性,就可以完成过滤功能了。方法可爱的地方在于我们可以不断的用它来合并查询条件,直到满意为止!
方法构建 StartsWith 过滤条件:
方法的返回值是一个字符串。运行上面的代码我们会得到字符串:
方法:
方法组合了不同的过滤条件。遗憾的是 TableQuery.CombineFilters 方法只有两个参数的重载,我们不能添加更多的 TableOperators 操作。
调用 TableQuery.CombineFilters 方法去组合上一个结果和新的条件。比如我们要把 Startswith 过滤条件和 PartitionKey 过滤条件组合起来就可以这么干:
代码,生成的结果为:
方法的主要工作就是把过滤条件组织成查询引擎能够识别的字符串。因而我们可以通过不断的叠加生成很复杂的过滤条件。
代码:
</span><span style="color: #0000ff">public</span> DateTime LogDate { <span style="color: #0000ff">get</span>; <span style="color: #0000ff">set</span><span style="color: #000000">; }
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> LogMessage { <span style="color: #0000ff">get</span>; <span style="color: #0000ff">set</span><span style="color: #000000">; }
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> ErrorType { <span style="color: #0000ff">get</span>; <span style="color: #0000ff">set</span><span style="color: #000000">; }
}
<span style="color: #0000ff">public <span style="color: #0000ff">class StartsWithByRowKey : IQuery<CloudTable,List{
<span style="color: #0000ff">private <span style="color: #0000ff">readonly <span style="color: #0000ff">string<span style="color: #000000"> startsWithString;
<span style="color: #0000ff">internal StartsWithByRowKey(<span style="color: #0000ff">string<span style="color: #000000"> partitionKey,<span style="color: #0000ff">string<span style="color: #000000"> startsWithString)
{
<span style="color: #0000ff">this.partitionKey =<span style="color: #000000"> partitionKey;
<span style="color: #0000ff">this.startsWithString =<span style="color: #000000"> startsWithString;
}
</span><span style="color: #0000ff">public</span> List<MyLogEntity><span style="color: #000000"> Execute(CloudTable coludTable)
{
</span><span style="color: #0000ff">var</span> query = <span style="color: #0000ff">new</span> TableQuery<MyLogEntity><span style="color: #000000">();
</span><span style="color: #0000ff">int</span> endIndex = startsWithString.Length - <span style="color: #800080">1</span><span style="color: #000000">;
Char lastChar </span>=<span style="color: #000000"> startsWithString[endIndex];
Char afterLastChar </span>= (<span style="color: #0000ff">char</span>)(lastChar + <span style="color: #800080">1</span><span style="color: #000000">);
</span><span style="color: #0000ff">string</span> endStr = startsWithString.Substring(<span style="color: #800080">0</span>,endIndex) +<span style="color: #000000"> afterLastChar;
</span><span style="color: #0000ff">string</span> startsWithCondition =<span style="color: #000000"> TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition(</span><span style="color: #800000">"</span><span style="color: #800000">RowKey</span><span style="color: #800000">"</span><span style="color: #000000">,startsWithString),endStr)
);
</span><span style="color: #0000ff">string</span> filterCondition =<span style="color: #000000"> TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition(</span><span style="color: #800000">"</span><span style="color: #800000">PartitionKey</span><span style="color: #800000">"</span><span style="color: #000000">,partitionKey),startsWithCondition
);
</span><span style="color: #0000ff">var</span> entities =<span style="color: #000000"> coludTable.ExecuteQuery(query.Where(filterCondition));
</span><span style="color: #0000ff">return</span><span style="color: #000000"> entities.ToList();
}
}
<span style="color: #0000ff">public <span style="color: #0000ff">interface IQuery<<span style="color: #0000ff">in TModel,<span style="color: #0000ff">out TResult><span style="color: #000000">
{
TResult Execute(TModel model);
}
查询 PartitionKey 为"201607",RowKey 以"16"开头的记录可以这么写:
代码简洁了很多,读起来也更清晰了(您还可以动手给 PartitionKey 也添加同样的功能)!