MindGame
MindGame

Reputation: 1251

Query Microsoft Azure by Entity Property Value

I want to query the table by property value. So lets say I have the following table

enter image description here

I want to create a query where I give me all records where PartitionKey = section3 FirstName = value3 Timestamp : last 12 hrs

I have the following code so far:

// 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 "asometrichub" table.
        CloudTable table = tableClient.GetTableReference("customer");

        TableQuery rangeQuery = new TableQuery().Where(
                  TableQuery.CombineFilters(
                      TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "section3"),
                      TableOperators.And,
                      TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.GreaterThanOrEqual, DateTime.UtcNow.AddHours(-12))));

Not sure how I filter on the FirstName=value3. Thank you!

Upvotes: 0

Views: 127

Answers (1)

Zhaoxing Lu
Zhaoxing Lu

Reputation: 6467

TableQuery.CombineFilters() can be called in a nested way:

// 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 "asometrichub" table.
CloudTable table = tableClient.GetTableReference("customer");

string combinedFilter = TableQuery.CombineFilters(
    TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "section3"),
    TableOperators.And,
    TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.GreaterThanOrEqual, DateTime.UtcNow.AddHours(-12)));

combinedFilter = TableQuery.CombineFilters(
    combinedFilter,
    TableOperators.And,
    TableQuery.GenerateFilterCondition("FirstName", QueryComparisons.Equal, "value3"));

TableQuery rangeQuery = new TableQuery().Where(combinedFilter);

Upvotes: 1

Related Questions