Joey Schluchter Joey Schluchter - 1 year ago 98
C# Question

TableQuery<T> from Azure TableStorage that filters on PartitionKey

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
var tableClient = _account.CreateCloudTableClient();
var table = tableClient.GetTableReference(entity.GetType().Name.ToLower());
var exQuery =
new TableQuery<T>().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal,

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.

Answer Source

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 ()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download