Azure 基础:自定义 Table storage 查询条件

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

MyLogEntity( pkey,.PartitionKey =.RowKey = }

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

,,));

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

startsWithCondition =,QueryComparisons.GreaterThanOrEqual,

"(RowKey ge ) and (RowKey lt )"

startStr = endIndex = startStr.Length - = Char afterLastChar = ()(lastChar + endStr = startStr.Substring(,endIndex) + startsWithCondition =

filterCondition =,

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

MyLogEntity( pkey,.PartitionKey =.RowKey =</span><span style="color: #0000ff"&gt;public</span> DateTime 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);
}

StartsWithByRowKey myStartsWithQuery = StartsWithByRowKey(, result = myStartsWithQuery.Execute(logTable);

相关文章

Microsoft云包括了Azure、PowerPlatform、Microsoft365、Git...
《WindowsAzurePlatform系列文章目录》 我们在使用AzureAPI...
微软免费使用一年的Azure虚拟机,默认提供了一个64G的磁盘,...
上篇请访问这里做一个能对标阿里云的前端APM工具(上)样本多...
一年一度的MicrosoftBuild终于来了,带来了非常非常多的新技...