动态生成用于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 (将#修改为@)