我将我现有的面向.NET Framework 4.6.2的类库移植到.NET Core 1.1。
看起来.NET Framework版本中的某些方法在.NET Core中不存在。两种这样的方法是table.CreateQuery
和table.ExecuteQuery
。
这是一个现有函数,它给我一个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中创建查询?
根据这个问题:Missing syncronous methods for dotnet core?,NetCore / Netstandard支持尚未包括API的Sync实现。
由于CreateQuery和ExecuteQuery都是Sync方法,因此我们无法在.NET Core中使用它,您只能使用ExecuteQuerySegmentedAsync,TableQuery,Fluent API并处理它返回的延续令牌。更多细节,您可以参考以下代码:
更新:
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;
}
希望这可以给你一些提示。
更新代码:
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;
}
我会在这篇文章中解决这些方便的扩展方法:)
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;
}
它们为您提供相同的功能,但使用ExecuteQueryASYNC作为方法名称