大佬教程收集整理的这篇文章主要介绍了Azure 基础:自定义 Table storage 查询条件,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
一文的基础上介绍如何自定义 Azure Table storage 的查询过滤条件。如果您还不太清楚 Azure Table storage 的基本用法,请先移步。
607表示2016年7月),RowKey 用来存放产生日志的天和时分秒毫秒(例如160934248492表示16号9点34分…)。在我们设计的 MyLogTable 中,天信息保存在 RowKey 的前两位。我们要做的就是过滤 RowKey 的前两位,也就是找到所有 RowKey 以"xx"开头的记录。这在字符串操作中称为 StartsWith。遗憾的是现有 Table storage 的接口中没有提供这种功能的方法,因此我们需要自己实现它(还好 TableQuery 的实现支持我们去扩展它)!本文将通过实现 StartsWith 过滤条件说明如何自定义 Azure Table storage 的查询过滤条件。
607" 并且 RowKey 等于"161148372454"的记录:
607') and (RowKey eq '161148372454')"。String 属性,就可以完成过滤功能了。
我们可以得出结论:以"abc"开头的字符串必定大于或等于"abc"且小于"abd"。OK,这就是我们构建 StartsWith 过滤条件的理论基础。
完全可以手动拼出这样的字符串,但我相信没有程序猿愿意这么干。所以我们要继续完善上面的方法:
Leoperators 操作。
可以继续调用 TableQuery.CombineFilters 方法去组合上一个结果和新的条件。比如我们要把 Startswith 过滤条件和 PartitionKey 过滤条件组合起来就可以这么干:
因而我们可以通过不断的叠加生成很复杂的过滤条件。
代码:
</span><span style="color: #0000ff">public</span> datetiR_376_11845@e 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><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);
}
607",RowKey 以"16"开头的记录可以这么写:
然后比较详细的说明了 StartsWith 过滤条件的思路及实现。主要是想通过 StartsWith 的实现来说明如何利用现有的类型及方法来实现自定义查询的过滤条件。对于有类似需求的朋友,希望能起到抛砖引玉的作用。
以上是大佬教程为你收集整理的Azure 基础:自定义 Table storage 查询条件全部内容,希望文章能够帮你解决Azure 基础:自定义 Table storage 查询条件所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。