如何在c#中构建通用方法以查询给定Azure表中的分区 [英] How can I Build a Generic Method in c# to Query a Partition in a Given Azure Table

查看:68
本文介绍了如何在c#中构建通用方法以查询给定Azure表中的分区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一种通用方法来返回给定Azure表中分区中的条目.看起来是这样的:

 公共类表:ITable{私有CloudStorageAccount storageAccount;公共表(){var storageAccountSettings = ConfigurationManager.ConnectionStrings ["AzureWebJobsStorage"].ToString();storageAccount = CloudStorageAccount.Parse(storageAccountSettings);}公共异步Task< IEnumerable< T>RetrieveAllInPartition< T>(字符串tableReference,字符串partitionKey)其中T:ITableEntity{var tableClient = storageAccount.CreateCloudTableClient();var table = tableClient.GetTableReference(tableReference);var query = new TableQuery< T>().Where(TableQuery.GenerateFilterCondition("PartitionKey",QueryComparisons.Equal,partitionKey));var results = await table.ExecuteQuerySegmentedAsync< T>(query,null);返回结果;}} 

这不会编译,我得到:

CS0310'T'必须是带有公共无参数的非抽象类型构造函数以便将其用作泛型中的参数"TElement"类型或方法
'CloudTable.ExecuteQuerySegmentedAsync(TableQuery,TableContinuationToken)'

有什么办法可以解决这个问题吗?

解决方案

您需要向通用参数添加 new()约束:

 公共异步任务< IEnumerable< T>>RetrieveAllInPartition< T>(字符串表参考,字符串partitionKey)其中T:ITableEntity,new() 

因为 ExecuteQuerySegmentedAsync 也具有此约束(如文档以了解有关 new的更多信息.()约束.

I'm trying to build a generic method to return entries in a partition in a given Azure table. This is how it looks:

public class Table : ITable
    {
        private CloudStorageAccount storageAccount;
        public Table()
        {
            var storageAccountSettings = ConfigurationManager.ConnectionStrings["AzureWebJobsStorage"].ToString();
            storageAccount = CloudStorageAccount.Parse(storageAccountSettings);
        }

        public async Task<IEnumerable<T>> RetrieveAllInPartition<T>(string tableReference, string partitionKey) where T : ITableEntity
        {
            var tableClient = storageAccount.CreateCloudTableClient();
            var table = tableClient.GetTableReference(tableReference);
            var query = new TableQuery<T>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey));
            var results = await table.ExecuteQuerySegmentedAsync<T>(query,null);
            return results;
        }
    }

This doesn't compile I get:

CS0310 'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TElement' in the generic type or method
'CloudTable.ExecuteQuerySegmentedAsync(TableQuery, TableContinuationToken)'

Any ideas how I can resolve this?

解决方案

You need to add new() constraint to your generic parameter:

public async Task<IEnumerable<T>> RetrieveAllInPartition<T>(string tableReference, string partitionKey) 
    where T : ITableEntity, new()

since ExecuteQuerySegmentedAsync also has this constraint (as can be seen in the documentation).

This constraint is required by ExecuteQuerySegmentedAsync because otherwise it won't be able to create instances of T for you. Please refer to the documentation to read more about the new() constraint.

这篇关于如何在c#中构建通用方法以查询给定Azure表中的分区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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