微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

动态生成用于Azure表存储的查询字符串

如何解决动态生成用于Azure表存储的查询字符串

我将实现一个函数,该函数将通过输入字典生成查询字符串。该词典中的元素是多个列名称及其值。查询条件取决于该字典的元素。请参见下面的代码


    public async Task<IList<T>> GetEntitiesAsync<T>(CloudStorageAccount storageAccount,string tableName,IDictionary<string,string> filterCondition) where T : ITableEntity,new()

    {
        var results = new List<T>();
        tableClient = storageAccount.CreateCloudTableClient();
        tableClient.DefaultRequestOptions = BatchRetryPolicy();
        cloudTable = tableClient.GetTableReference(tableName);

        // Generate the first n-1 elements of dictionary and convert them to a string 
        var first = filterCondition.Take(filterCondition.Count - 1).Select(x => TableQuery.GenerateFilterCondition(x.Key,QueryComparisons.Equal,x.Value));
        StringBuilder sb = new StringBuilder();
        foreach (var element in first)
        {
            sb.Append(element+TableOperators.And);
        }
        // Move the last TableOperators.And
        sb.Remove(sb.Length - 3,3);

        // Generate the whole query string
        TableQuery<T> query = new TableQuery<T>().Where(TableQuery.CombineFilters(
                                                        sb.ToString(),TableOperators.And,TableQuery.GenerateFilterCondition(filterCondition.LastOrDefault().Key,filterCondition.LastOrDefault().Value)));

        TableContinuationToken continuationToken = null;
        do
        {
            var response = await cloudTable.ExecuteQuerySegmentedAsync(query,continuationToken);
            continuationToken = response.ContinuationToken;
            results.AddRange(response.Results);
        } while (continuationToken != null);
        return results;
    }

我的问题是:是否有更好的方法生成TableQuery.CombineFilter()方法的第一个参数? 通过使用此字典的前n-1个元素并将它们转换为stringbuilder,我的使用方式看起来很愚蠢。有什么好主意吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。