3
votes

I want to create a generic function to retrieve an entity from azure table storage.Now i have multiple entities in a table, each having different properties. So i want to retrieve a single entity from a partition filtering by the property of the entity. I have generated random row keys (from 1-100). So retrieving using partition n row key is not an option for me. Want to retrieve using partition key and 1or2 properties of the entity? how to do that?

I have written this to retrieve all values from a partition:

    public object GetEntity(string partitionKey, string rowKey)
{  
    CloudTable table = tableClient.GetTableReference(Constants.STORAGETABLENAME);
    TableOperation tableOperation = null;
    switch (partitionKey)
    {

        case Constants.PARTITION_RESPONSEFILTERVALUE:
            tableOperation = TableOperation.Retrieve<ResponseFilterParamsEntity>(partitionKey, rowKey);
            break;
        case Constants.PARTITION_RESPONSEFILTER:
            tableOperation = TableOperation.Retrieve<ResponseFilterFieldsEntity>(partitionKey, rowKey);
            break;
        case Constants.PARTITION_REDISCACHE:
            tableOperation = TableOperation.Retrieve<RedisCacheEntity>(partitionKey, rowKey);
            break;                
        default:break;
    }
    return table.Execute(tableOperation).Result;
}

This function filters using partition key and only 1 property :

public List<T> GetCustEntities<T>(string partitionKey, string strFilter, string strFilterValue, T entity) where T : TableEntity, new()
{
    try
    {
        var table = tableClient.GetTableReference(Constants.STORAGETABLENAME);
        var Q1 = new TableQuery<T>().Where(TableQuery.CombineFilters(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, partitionKey),
                                                                        TableOperators.And,
                                                                        TableQuery.GenerateFilterCondition(strFilter, QueryComparisons.Equal, strFilterValue)));
        var results = table.ExecuteQuery(Q1).Select(ent => (T)ent).ToList();

        return results;

    }
    catch (StorageException ex)
    {
        return null;
    }
}
1
Can you provide some code and show us what you have done so far?T. Jung
This is my query to retrieve all entries from different models :SDR

1 Answers

0
votes

https://docs.microsoft.com/en-us/azure/storage/storage-dotnet-how-to-use-tables#retrieve-a-range-of-entities-in-a-partition

You can replace RowKey with any other properties in the example of the document above, for instance:

// Retrieve the storage account from the connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));

// Create the table client.
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();

// Create the CloudTable object that represents the "people" table.
CloudTable table = tableClient.GetTableReference("people");

// Create the table query.
TableQuery<CustomerEntity> rangeQuery = new TableQuery<CustomerEntity>().Where(
TableQuery.CombineFilters(
    TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "Smith"),
    TableOperators.And,
    TableQuery.GenerateFilterCondition("PhoneNumber", QueryComparisons.LessThan, "1345678")));

// Loop through the results, displaying information about the entity.
foreach (CustomerEntity entity in table.ExecuteQuery(rangeQuery))
{
    Console.WriteLine("{0}, {1}\t{2}\t{3}", entity.PartitionKey, entity.RowKey,
    entity.Email, entity.PhoneNumber);
}

Code for your example:

public List<T> GetCustEntities<T>(string partitionKey, Dictionary<string, string> propertyFilters) where T : TableEntity, new()
{
    var table = tableClient.GetTableReference(Constants.STORAGETABLENAME);
    var pkFilter = TableQuery.GenerateFilterCondition(TableConstants.PartitionKey, QueryComparisons.Equal, partitionKey;
    var combinedFilter = pkFilter;
    foreach (var kvp in propertyFilters)
    {
        var newFilter = TableQuery.GenerateFilterCondition(kvp.Key, QueryComparisons.Equal, kvp.Value);
        combinedFilter = TableQuery.CombineFilters(combinedFilter, TableOperators.And, newFilter);
    }

    var query = new TableQuery<T>().Where(combinedFilter);
    var results = table.ExecuteQuery(query).ToList();

    return results;
}