c# - 使用表达式查询 Azure 表存储

标签 c# azure azure-table-storage

我希望通过将表达式传递到我的存储库而不是 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();
}

理想用途:

    var data = await _filterRepository.Get(x => x.PartitionKey.Equals("examplepartitionkey"));

我在 CreateQueryCreateQuery 上看不到任何接受表达式的方法,仅接受文本。

我还想看看是否可以将表达式转换为过滤文本,例如 PartitionKey eq examplepartitionkey,但没有成功。

有人可以帮我通过表达式查询azure表存储吗?

我使用的 SDK 是 Microsoft.Azure.Cosmos.Table v1.0.7.0;

最佳答案

由于 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
        );
    }

关于c# - 使用表达式查询 Azure 表存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69539205/

相关文章:

azure - 使用 Add-StorageTableRow 在 azure 表中添加行

c#无参数扩展方法

c# - 为什么 BinaryFormatter 试图在可序列化类上序列化事件?

c# - 静态对象的扩展方法

azure - 将pfx证书上传到Azure应用程序网关时如何获取名称?

azure - Windows Azure 表 - 行数?

c# - 派生类列表上的泛型函数

c# - Azure ServiceBus - 多次读取同一消息

如果不删除以前的部署,Azure 辅助角色部署将失败

azure - 如何查看 Windows Azure 表存储中每个表中有多少行?