I'm looking to query table storage by passing an expression into my repository instead of a TableQuery, i'm aiming for this:
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.
I've also looked to see if I can convert the expression to filter text such as PartitionKey eq examplepartitionkey
, but no luck.
Can someone please help me query azure table storage via expression?
The SDK i'm using is Microsoft.Azure.Cosmos.Table v1.0.7.0;
CodePudding user response:
Assuming using the Cosmos table driver against Azure Table Storage, you should be able to something along the lines of
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.
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.
I do agree that the number of, and guidance toward, client libraries for Azure Table storage is becoming quite frustrating.