I'm trying to abstract geting all entities from a Table by partitionKey, like so:
public List<T> GetEntities<T>(string partitionKey, T entity) where T : TableEntity
{
try
{
var tableClient = _account.CreateCloudTableClient();
var table = tableClient.GetTableReference(entity.GetType().Name.ToLower());
var exQuery =
new TableQuery<T>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal,
partitionKey));
var results = table.ExecuteQuery(exQuery).Select(ent => (T) ent).ToList();
return results;
}
catch (StorageException ex)
{
//TODO: Add more trace info
Trace.TraceInformation("Unable to retrieve entity based on query specs");
return null;
}
}
However, It's failing on the
new TableQuery<T>
because the TElement does not have a parameterless constructor.
A class which represents a query against a specified table. A TableQuery<T> instance aggregates the query parameters to use when the query is executed.
Yes, the Table API supports OData query and LINQ query.
As you also mentioned in your question, T must have a parameterless constructor. Hence, please change the definition of your method as follows:
public List<T> GetEntities<T>(string partitionKey, T entity) where T : TableEntity, new ()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With