.NET Core中的Azure表存储CreateQuery [英] Azure Table Storage CreateQuery in .NET Core

查看:86
本文介绍了.NET Core中的Azure表存储CreateQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将针对.NET Framework 4.6.2的现有类库移植到.NET Core 1.1.

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

.NET Core中似乎没有.NET Framework版本中可用的某些方法.两种方法是table.CreateQuerytable.ExecuteQuery.

Looks like some of the methods that are available in .NET Framework version are not there in .NET Core. Two such methods are table.CreateQuery and table.ExecuteQuery.

这是一个现有函数,给我一个CreateQuery错误:

Here's an existing function that's giving me an error for CreateQuery:

public T Get<T>(string partitionKey, string rowKey, string tableName) where T : ITableEntity, new()
            => getTable(tableName).CreateQuery<T>().Where(r => r.PartitionKey == partitionKey && r.RowKey == rowKey).FirstOrDefault();

如何在.NET Core中创建查询?

How do I create query in .NET Core?

推荐答案

根据此问题:

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

由于CreateQuery和ExecuteQuery都是Sync方法,因此我们无法在.NET Core中使用它,因此只能使用ExecuteQuerySegmentedAsync,TableQuery,Fluent API并处理它返回的延续令牌. 更多详细信息,您可以参考以下代码:

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:

更新:

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.

更新代码:

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;
}

这篇关于.NET Core中的Azure表存储CreateQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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