[英]What package should I use for Azure Tables when migrating away from "WindowsAzure.Storage"
I am currently migrating a project that was written for .NET Fx 4.8 using ASP.NET WebApi to .NET 5.0.我目前正在将使用 ASP.NET WebApi 为 .NET Fx 4.8 编写的项目迁移到 .NET 5.0。 This project makes heavy use of the package WindowsAzure.Storage for accessing BLOBs, tables and queues on a Azure Storage account.该项目大量使用 package WindowsAzure.Storage来访问 Azure 存储帐户上的 BLOB、表和队列。 This package has been deprecated for a while now, stating that is has been split up into several packages.这个 package 现在已经被弃用了一段时间,说明它已经被分成几个包。
For Queues and Blobs it is quite clear what I should migrate to.对于队列和 Blob,我应该迁移到什么位置非常清楚。 But there seems to be a lot of confusion regarding Azure Tables however.但是,关于 Azure 表似乎有很多混淆。
Anyway: I found the API of Azure.Data.Tables quite similar to WindowsAzure.Storage , I just had to make a few tweaks here and there.无论如何:我发现 Azure.Data.Tables 的API与WindowsAzure.Storage非常相似,我只需要在这里和那里进行一些调整。
But what I am totally missing now is the classes QueryComparisons and TableQuery .但是我现在完全缺少的是QueryComparisons和TableQuery类。 Are they just gone?他们刚刚走了吗? Is there are replacement?有替代品吗?
An example of the code to migrate would be:要迁移的代码示例如下:
var part1 =
TableQuery.GenerateFilterConditionForGuid(
nameof(SomeEntity.Id),
QueryComparisons.Equal,
idValue));
var part2 =
TableQuery.GenerateFilterConditionForGuid(
nameof(SomeEntity.Category)
QueryComparisons.Equal,
category));
return TableQuery.CombineFilters(part1, TableOperators.And, part2);
I came across the same problem.我遇到了同样的问题。 Yes, TableQuery
from Microsoft.Azure.Cosmos.Tables
is gone, and the references in documentation to TableOdataFilter
are useless as it doesn't exist.是的,来自Microsoft.Azure.Cosmos.Tables
的TableQuery
已经消失,并且文档中对TableOdataFilter
的引用是无用的,因为它不存在。
Looking at this, I found the easiest way to query Azure Storage Tables using the Azure.Data.Tables
package is to use the .Query<T>([predicate])
method .看着这个,我发现使用Azure.Data.Tables
package 方法查询 Azure 存储表的最简单方法是使用.Query<T>([predicate])
方法。 eg例如
TableClient customers;
var example = customers.Query<Customer>(c => c.Name == "Microsoft");
There is also a .QueryAsync<T>(..)
async version.还有一个.QueryAsync<T>(..)
异步版本。
If you were building queries with the TableQuery
there is no direct alternative, but you can use something like PredicateBuilder to combine .And()
and .Or()
expressions to create the predicate.如果您使用TableQuery
构建查询,则没有直接的替代方法,但您可以使用PredicateBuilder之类的东西来组合.And()
和.Or()
表达式来创建谓词。
I modified this to create a fluent version:我修改了这个以创建一个流畅的版本:
/// <summary>
/// Helper class to build predicates fluently
/// </summary>
/// <remarks>
/// Adapted from source: http://www.albahari.com/nutshell/predicatebuilder.aspx
/// </remarks>
public class PredicateBuilder<T>
{
private Expression<Func<T, bool>> expression;
/// <summary>
/// Add an OR clause
/// </summary>
/// <param name="expr">new expression</param>
/// <returns></returns>
public PredicateBuilder<T> Or(Expression<Func<T, bool>> expr)
{
if (expression == null)
expression = expr;
else
{
var invokedExpr = Expression.Invoke(expr, expression.Parameters.Cast<Expression>());
expression = Expression.Lambda<Func<T, bool>>(Expression.OrElse(expression.Body, invokedExpr), expression.Parameters);
}
return this;
}
/// <summary>
/// Add an AND clause
/// </summary>
/// <param name="expr">new expression</param>
/// <returns></returns>
public PredicateBuilder<T> And(Expression<Func<T, bool>> expr)
{
if (expression is null)
expression = expr;
else
{
var invokedExpr = Expression.Invoke(expr, expression.Parameters.Cast<Expression>());
expression = Expression.Lambda<Func<T, bool>>(Expression.AndAlso(expression.Body, invokedExpr), expression.Parameters);
}
return this;
}
/// <summary>
/// Return resulting expression
/// </summary>
/// <returns></returns>
public Expression<Func<T, bool>> ToPredicate()
{
if (expression is null)
return (T value) => true;
return expression;
}
}
Example usage:示例用法:
var query = new PredicateBuilder<Customer>();
if(name != null)
query.And(c => c.Name == name);
if (isActive == false)
query.And(c => c.IsActive == false);
Hope this helps希望这可以帮助
You should probably have read a little further in that blog post and the related docs :您可能应该在该博客文章和相关文档中进一步阅读:
Querying Table Entities查询表实体
The TableClient allows the user to create custom queries using OData filters. TableClient 允许用户使用 OData 过滤器创建自定义查询。
string MyPK = "markers";
string MyRK = "id-001";
string filter = TableOdataFilter.Create($"PartitionKey eq {MyPK} or RowKey eq {MyRK}")
Pageable<TableEntity> entities = tableClient.Query<TableEntity>(filter: filter);
foreach (TableEntity entity in entities)
{
Console.WriteLine($"{entity.GetString("Product")}: {entity.GetInteger("Count")}");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.