Azure   发布时间:2019-11-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Azure 基础:自定义 Table storage 查询条件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

一文的基础上介绍如何自定义 Azure Table storage 的查询过滤条件。如果您还不太清楚 Azure Table storage 的基本用法,请先移步

{ { } MyLogEntity(String pkey,String{ .PartitionKey =.RowKey = }

607表示2016年7月),RowKey 用来存放产生日志的天和时分秒毫秒(例如160934248492表示16号9点34分…)。在我们设计的 MyLogTable 中,天信息保存在 RowKey 的前两位。我们要做的就是过滤 RowKey 的前两位,也就是找到所有 RowKey 以"xx"开头的记录。这在字符串操作中称为 StartsWith。遗憾的是现有 Table storage 的接口中没有提供这种功能的方法,因此我们需要自己实现它(还好 TableQuery 的实现支持我们去扩展它)!本文将通过实现 StartsWith 过滤条件说明如何自定义 Azure Table storage 的查询过滤条件。

TableQuery query = TableQuery,QueryComparisons.Equal,607); queryResult = logTable.ExecuteQuery(query);

607" 并且 RowKey 等于"161148372454"的记录:

,607Leoperators.And,TableQuery.GenerateFilterCondition(,));

607') and (RowKey eq '161148372454')"。String 属性,就可以完成过滤功能了。

“abc” == “abc” << “abca” << “abcz” < “abd”

我们可以得出结论:以"abc"开头的字符串必定大于或等于"abc"且小于"abd"。OK,这就是我们构建 StartsWith 过滤条件的理论基础。

String startsWithCondition =,QueryComparisons.GreaterThanOrequal,s.LessThan,);

"(RowKey ge ) and (RowKey lt )"

完全可以手动拼出这样的字符串,但我相信没有程序猿愿意这么干。所以我们要继续完善上面的方法:

String startStr = endIndex = startStr.Length - = Char afterLastChar = ()(lastChar + ); String endStr = startStr.SubString(,endIndeX) +String startsWithCondition =);

Leoperators 操作。

801_10156@调用 TableQuery.CombineFilters 方法去组合上一个结果和新的条件。比如我们要把 Startswith 过滤条件和 PartitionKey 过滤条件组合起来就可以这么干:

String filterCondition =,);

(PartitionKey eq 607) and ((RowKey ge ) and (RowKey lt ))

因而我们可以@R_616_10402@的叠加生成很复杂的过滤条件。

代码:

{ { } MyLogEntity(String pkey,String{ .PartitionKey =.RowKey =</span><span style="color: #0000ff"&gt;public</span> datetiR_302_11845@e LogDate { <span style="color: #0000ff"&gt;get</span>; <span style="color: #0000ff"&gt;set</span><span style="color: #000000"&gt;; } </span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;String</span> Logmessage { <span style="color: #0000ff"&gt;get</span>; <span style="color: #0000ff"&gt;set</span><span style="color: #000000"&gt;; } </span><span style="color: #0000ff"&gt;public</span> <span style="color: #0000ff"&gt;String</span> ErrorType { <span style="color: #0000ff"&gt;get</span>; <span style="color: #0000ff"&gt;set</span><span style="color: #000000"&gt;; }

}<span style="color: #0000ff">public <span style="color: #0000ff">class StartsWithByRowKey : IQuery<CloudTable,List><span style="color: #000000">
{
<span style="color: #0000ff">private <span style="color: #0000ff">readonly <span style="color: #0000ff">String<span style="color: #000000"> partitionKey;
<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"&gt;public</span> List<MyLogEntity><span style="color: #000000"&gt; Execute(CloudTable coludTablE)
{
    </span><span style="color: #0000ff"&gt;var</span> query = <span style="color: #0000ff"&gt;new</span> TableQuery<MyLogEntity><span style="color: #000000"&gt;();

    </span><span style="color: #0000ff"&gt;int</span> endIndex = startsWithString.Length - <span style="color: #800080"&gt;1</span><span style="color: #000000"&gt;;
    Char lastChar </span>=<span style="color: #000000"&gt; startsWithString[endIndex];
    Char afterLastChar </span>= (<span style="color: #0000ff"&gt;char</span>)(lastChar + <span style="color: #800080"&gt;1</span><span style="color: #000000"&gt;);
    </span><span style="color: #0000ff"&gt;String</span> endStr = startsWithString.SubString(<span style="color: #800080"&gt;0</span>,endIndeX) +<span style="color: #000000"&gt; afterLastChar;

    </span><span style="color: #0000ff"&gt;String</span> startsWithCondition =<span style="color: #000000"&gt; TableQuery.CombineFilters(
         TableQuery.GenerateFilterCondition(</span><span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;RowKey</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;,startsWithString),endStr)
    );

    </span><span style="color: #0000ff"&gt;String</span> filterCondition =<span style="color: #000000"&gt; TableQuery.CombineFilters(
         TableQuery.GenerateFilterCondition(</span><span style="color: #800000"&gt;"</span><span style="color: #800000"&gt;PartitionKey</span><span style="color: #800000"&gt;"</span><span style="color: #000000"&gt;,partitionKey),startsWithCondition
    );

    </span><span style="color: #0000ff"&gt;var</span> entities =<span style="color: #000000"&gt; coludTable.ExecuteQuery(query.Where(filterCondition));
    </span><span style="color: #0000ff"&gt;return</span><span style="color: #000000"&gt; 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);
}

607",RowKey 以"16"开头的记录可以这么写:

StartsWithByRowKey myStartsWithQuery = StartsWithByRowKey(607,); List result = myStartsWithQuery.Execute(logTablE);

然后比较详细的说明了 StartsWith 过滤条件的思路及实现。主要是想通过 StartsWith 的实现来说明如何利用现有的类型及方法来实现自定义查询的过滤条件。对于有类似需求的朋友,希望能起到抛砖引玉的作用。

大佬总结

以上是大佬教程为你收集整理的Azure 基础:自定义 Table storage 查询条件全部内容,希望文章能够帮你解决Azure 基础:自定义 Table storage 查询条件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: