.NET Core中的Azure表存储CreateQuery

编程入门 行业动态 更新时间:2024-10-23 01:29:48
本文介绍了.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.CreateQuery和table.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?

推荐答案

根据此问题:是否缺少用于dotnet核心的同步方法? NetCore/Netstandard支持尚不包括API的Sync实现.

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

本文发布于:2023-11-16 22:07:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607661.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Core   NET   CreateQuery   Azure

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!