Azure Storage table query

In an Azure Storage table you have a partition key and a row key. Azure Storage tables are indexed by these two columns which is important for query performance. In the code snippet below, you can see two things. First of all, it’s possible to query Azure Storage tables by other columns than partition key and row key. Secondly, you can query Azure Storage tables using multiple columns. First you create a Filter and then you create a query using that filter. Note that you can create a query with a filter on three columns by nesting a combined filter within another combined filter. Finally you can see an example of ordering the query results. In this case I only need the most recent record. That’s why I use the FirstOrDefault statement. Note that AGABijlage is set to null when no AGABijlage is found.

Code:

string integrationId = “1000484844”;
string formCode = “AHakFotov100”;

// Retrieve the storage account from the connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(“DefaultEndpointsProtocol=https;AccountName=…;AccountKey=…);

// Create the table client + reference table
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference(“AGABijlagen”);

string partitionKeyFilter = TableQuery.GenerateFilterCondition(“PartitionKey”, QueryComparisons.Equal, integrationId);
string formCodeFilter = TableQuery.GenerateFilterCondition(“FormCode”, QueryComparisons.Equal, formCode);
string finalFilter = TableQuery.CombineFilters(partitionKeyFilter, TableOperators.And, formCodeFilter);

// Get records for IntegrationId from table AGABijlagen
TableQuery<AGABijlage> query = new TableQuery<AGABijlage>().Where(finalFilter);

AGABijlage agaBijlage = (AGABijlage)table.ExecuteQuery(query).OrderByDescending(aga => aga.Timestamp).FirstOrDefault<AGABijlage>();
if (agaBijlage != null)
{
Console.WriteLine(String.Format(“{0} {1} {2}”, agaBijlage.RowKey, agaBijlage.Timestamp, agaBijlage.Blob));
Console.WriteLine(“Press any key to exit”);
Console.ReadLine();
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *