如何使用分区n行密钥从azure表中检索单个实体

时间:2017-08-03 07:33:48

标签: .net azure-table-storage

我想创建一个泛型函数来从azure表存储中检索实体。现在我在表中有多个实体,每个实体都有不同的属性。所以我想通过实体的属性从分区过滤中检索单个实体。 我已经生成了随机行键(从1-100开始)。因此,使用分区n行键检索不是我的选择。 想要使用分区键和实体的1or2属性进行检索吗? 怎么做?

我已经写了这个来从分区中检索所有值:

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

此功能使用分区键过滤,只有1个属性:

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 个答案:

答案 0 :(得分:1)

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

您可以将RowKey替换为上述文档示例中的任何其他属性,例如:

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

您的示例代码:

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