TableQuery< T>从Azure的TableStorage上PartitionKey过滤器 [英] TableQuery<T> from Azure TableStorage that filters on PartitionKey

查看:257
本文介绍了TableQuery< T>从Azure的TableStorage上PartitionKey过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过抽象歌厅partitionKey所有实体从表中,像这样:

I'm trying to abstract geting all entities from a Table by partitionKey, like so:

public List<T> GetEntities<T>(string partitionKey, T entity) where T : TableEntity
    {
        try
        {
            var tableClient = _account.CreateCloudTableClient();
            var table = tableClient.GetTableReference(entity.GetType().Name.ToLower());
            var exQuery =
                new TableQuery<T>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal,
                                                                             partitionKey));

            var results = table.ExecuteQuery(exQuery).Select(ent => (T) ent).ToList();
            return results;
        }
        catch (StorageException ex)
        {
            //TODO: Add more trace info
            Trace.TraceInformation("Unable to retrieve entity based on query specs");
            return null;
        }
    }

然而,它的失败在

However, It's failing on the

new TableQuery<T>

因为TElement没有一个参数的构造函数。

because the TElement does not have a parameterless constructor.

推荐答案

当你还在你的问题中提到,T必须有一个参数的构造函数。因此,请改变你的方法的定义如下:

As you also mentioned in your question, T must have a parameterless constructor. Hence, please change the definition of your method as follows:

public List<T> GetEntities<T>(string partitionKey, T entity) where T : TableEntity, new ()

这篇关于TableQuery&LT; T&GT;从Azure的TableStorage上PartitionKey过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆