Azure Table Storage CreateQuery in .NET Core

前端 未结 2 1211
时光说笑
时光说笑 2021-02-07 05:23

I\'m porting my existing class library that targets .NET Framework 4.6.2 to .NET Core 1.1.

Looks like some of the methods that are available in .NET Framework version ar

相关标签:
2条回答
  • 2021-02-07 06:05

    I'll tack these handy extension methods on to this post :)

    public static async System.Threading.Tasks.Task<IEnumerable<DynamicTableEntity>> ExecuteQueryAsync(this CloudTable table, TableQuery query)
    {
        TableContinuationToken token = null;
        var retVal = new List<DynamicTableEntity>();
        do
        {
            var results = await table.ExecuteQuerySegmentedAsync(query, token);
            retVal.AddRange(results.Results);
            token = results.ContinuationToken;
        } while (token != null);
    
        return retVal;
    }
    
    public static async System.Threading.Tasks.Task<IEnumerable<T>> ExecuteQueryAsync<T>(this CloudTable table, TableQuery<T> query) where T : ITableEntity, new()
    {
        TableContinuationToken token = null;
        var retVal = new List<T>();
        do
        {
            var results = await table.ExecuteQuerySegmentedAsync(query, token);
            retVal.AddRange(results.Results);
            token = results.ContinuationToken;
        } while (token != null);
    
        return retVal;
    }
    

    They give you back the same functionality but with ExecuteQueryASYNC as the method name

    0 讨论(0)
  • 2021-02-07 06:15

    According to this question: Missing syncronous methods for dotnet core?,NetCore/Netstandard support does not yet include Sync implementation of the APIs.

    Since CreateQuery and ExecuteQuery are all Sync method, so we couldn’t use it in .NET Core, you could only use ExecuteQuerySegmentedAsync,TableQuery, Fluent API and handle the continuation token it returns. More details, you could refer to follow codes:


    Update:

    public static void Main(string[] args)
    {
        var result = Get<BookTest3>("Aut_Fantasy", "Cert-0000000020", "DifferenetPartitionTest");
        Console.Write(result.PartitionKey);
        Console.Read();
    }
    
    public static T Get<T>(string partitionKey, string rowKey, string tableName) where T : ITableEntity, new()
    {
        CloudTable table = ConnectToTable(tableName);
        TableQuery<T> employeeQuery = new TableQuery<T>().Where(
            TableQuery.CombineFilters(
                TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey),
                TableOperators.And,
                TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.LessThan, rowKey))
            ).Take(1);
        var re = new T();
            TableContinuationToken continuationToken = null;
        do
        {
            Task<TableQuerySegment<T>> employees = table.ExecuteQuerySegmentedAsync(employeeQuery, continuationToken);
            TableQuerySegment<T> employeess = employees.Result;
            re= employeess.FirstOrDefault();
            continuationToken = employeess.ContinuationToken;
        } while (continuationToken != null);
        return re;
    }
    

    Hope this could give you some tips.


    Update code:

    public static void Main(string[] args)
    {
        var tas = Get<BookTest3>("Aut_Fantasy", "Cert-0000000020", "DifferenetPartitionTest");
        var result = tas.Result;
        Console.Write(result.PartitionKey);
        Console.Read();
    }
    
    public async static Task<T> Get<T>(string partitionKey, string rowKey, string tableName) where T : ITableEntity, new()
    {
        //new T();
        CloudTable table = ConnectToTable(tableName);
        TableQuery<T> employeeQuery = new TableQuery<T>().Where(
            TableQuery.CombineFilters(
                TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey),
                TableOperators.And,
                TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.LessThan, rowKey))
            ).Take(1);
        var re = new T();
            TableContinuationToken continuationToken = null;
        do
        {
            var employees = await table.ExecuteQuerySegmentedAsync(employeeQuery, continuationToken);
    
            re = employees.FirstOrDefault();
            continuationToken = employees.ContinuationToken;
        } while (continuationToken != null);
        return re;
    }
    
    0 讨论(0)
提交回复
热议问题