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;
}
}