Wednesday, October 29, 2014

Querying Table Storage

Querying the Table

We’ve successfully added data to the table both via code and by importing with a tool.  While the Storage tools allow us to verify that we uploaded what we expected from our code, the applications are likely to be using code to retrieve their data.  Let’s look at a very simple sample query:  To do this we replace the code in the Try catch of the Main method with the following code:
When you execute this code, you’ll see that the entity was retrieved and the product name was displayed.  In this case you will see that we queried by the partition key and row key, so essentially we were performing a lookup by the entity’s primary key.  There are numerous ways of querying for the data you have in storage, but knowing the exact partition key and row key will always be the fastest.
For the new Table Service Layer you can also use the lightweight query class TableQuery.  This class allows you to construct a query to send to the server that is more complex than simply asking for the PartitionKey and RowKey; however, the syntax is not as easy as working with LINQ.  If our data had an integer property for StockOnHand we could search for all items in the Baseball category that we have in stock using the following: