Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
    {
        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.

like image 660
Joey Schluchter Avatar asked Nov 20 '12 18:11

Joey Schluchter


People also ask

What is TableQuery?

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.

Which of the below queries are supported by table API?

Yes, the Table API supports OData query and LINQ query.


1 Answers

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 ()
like image 169
Serdar Ozler Avatar answered Oct 12 '22 23:10

Serdar Ozler