chuckd
chuckd

Reputation: 14520

How can I query from a list of partition keys in Azure table storage

I have an Azure table where the partition key is the profile id of a user logged into the website, so if I have a list of user ids I need to fetch if there is a partition key in the table with that id. I want to return a list of ids(partition keys) that were found.

I can't seem to find any good info online, that says I can do this easily!

If needed I can just move to Sql Server instead of Azure Storage Tables

I want to do something like this

 var query = new TableQuery<ConnectionEntity>()
.Where(TableQuery.GenerateFilterCondition("PartitionKey" , QueryComparisons.Equal, "1 or 3 or 4"));

 var queryResult = table.ExecuteQuery(query).ToList();

Upvotes: 6

Views: 3031

Answers (1)

Brendan Green
Brendan Green

Reputation: 11904

You want to combine multiple filters.

var filterOne = TableQuery.GenerateFilterCondition("PartitionKey" , QueryComparisons.Equal, "1");

var filterTwo = TableQuery.GenerateFilterCondition("PartitionKey" , QueryComparisons.Equal, "2");

var filterThree = TableQuery.GenerateFilterCondition("PartitionKey" , QueryComparisons.Equal, "3");

var combinedFilters = TableQuery.CombineFilters(
                    TableQuery.CombineFilters(
                        filterOne,
                        TableOperators.Or,
                        filterTwo),
                    TableOperators.And, filter3);

var query = new TableQuery<ConnectionEntity>()
    .Where(combinedFilters);

Alternatively, it might be easier to use CreateQuery<T>() against your CloudTable, which will let you write LINQ queries:

_cloudTable.CreateQuery<ConnectionEntity>()
    .AsQueryable()
    .Where(w => w.PartitionKey == "1" 
        || w.PartitionKey == "2" 
        || w.PartitionKey == "3");

Upvotes: 4

Related Questions