[英]Querying Azure Table Storage with an expression
I'm looking to query table storage by passing an expression into my repository instead of a TableQuery, i'm aiming for this:我希望通过将表达式而不是 TableQuery 传递到我的存储库来查询表存储,我的目标是:
public async Task<IEnumerable<FiltersEntity>> Get(Expression<Func<FiltersEntity, bool>> query)
{
var table = _tableStorageConnector.CreateTableIfNotExists(_tableName);
return await table.CreateQuery<FiltersEntity>().Where(query).ToList();
}
Ideal usage:理想用法:
var data = await _filterRepository.Get(x => x.PartitionKey.Equals("examplepartitionkey"));
I can't see any methods on CreateQuery
or CreateQuery
which accept an expression, only text.我在
CreateQuery
或CreateQuery
上看不到任何接受表达式的方法,只有文本。
I've also looked to see if I can convert the expression to filter text such as PartitionKey eq examplepartitionkey
, but no luck.我还查看了是否可以将表达式转换为过滤文本,例如
PartitionKey eq examplepartitionkey
,但没有运气。
Can someone please help me query azure table storage via expression?有人可以帮我通过表达式查询 azure 表存储吗?
The SDK i'm using is Microsoft.Azure.Cosmos.Table v1.0.7.0;我使用的 SDK 是 Microsoft.Azure.Cosmos.Table v1.0.7.0;
Assuming using the Cosmos table driver against Azure Table Storage, you should be able to something along the lines of假设对 Azure 表存储使用 Cosmos 表驱动程序,您应该能够执行以下操作
public async Task<IEnumerable<MyTable>> MyQuery(Expression<Func<MyTable, bool>> predicate)
{
var tableClient = new CloudTableClient(
new Uri("..."),
new StorageCredentials(".."));
var table = tableClient.GetTableReference("MyTable");
var tableQuery = table.CreateQuery<MyTable>()
.Where(predicate)
.AsTableQuery(); // << convert back from IQueryable to TableQuery
var onePage = await table.ExecuteQuerySegmentedAsync(tableQuery, null);
return onePage.Results;
}
Where MyTable
implements ITableEntity
and extends custom columns.其中
MyTable
实现ITableEntity
并扩展自定义列。
Note if the query is likely to return more than one page of data, then you'll need to replace the null with a TableContinuationToken and collect the pages in a loop.请注意,如果查询可能返回一页以上的数据,那么您需要用TableContinuationToken替换 null 并在循环中收集页面。
I do agree that the number of, and guidance toward, client libraries for Azure Table storage is becoming quite frustrating.我确实同意 Azure 表存储的客户端库的数量和指导变得非常令人沮丧。
Since Google and Bing can't seem to understand the difference with WATS vs Azure.Data.Tables I am leaving this example here for others searching for how to use the new SDK v12由于 Google 和 Bing 似乎无法理解 WATS 与 Azure.Data.Tables 的区别,因此我将这个示例留在这里,以供其他人搜索如何使用新的 SDK v12
AsyncPageable<SimpleEntity> EventsQueryAsyncTask(CancellationToken ct)
{
var rowKeyStart = partition.EventVersionRowKey(startVersion);
var rowKeyEnd = partition.EventVersionRowKey(startVersion + sliceSize - 1);
var partitioned = partition.AsPartitionExpression<SimpleEntity>();
var expression = partitioned.Body;
var parameters = partitioned.Parameters[0]; // NOTE: use this if you have parameters to pass into your expression otherwise remove this variable...
var lambda = Expression.Lambda<Func<SimpEntity, bool>>
(
Expression.AndAlso
(
expression,
Query.WithinRangeAndLowerBoundEquality<SimpEntity>(rowKeyStart, rowKeyEnd).Body
),
parameters // see comment above...
);
return client.QueryAsync
(
filter: lambda,
maxPerPage: 100,
select: null, // or pass in an IEnumerable<string> of columns you want to limit the projection to...
cancellationToken: ct
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.