Azure Table Storage – DataServiceQueryException when using Count()

I have been expanding the Azure Storage Sample project. I came across an interesting limitation with the OData query projection currently supported by the Azure Storage Client. My desire was to have a rowkey that increments per partionkey entry. The concept would be each row would map to a note on a customer order. The partition would map to an order id and each rowkey would be an incrementing integer (like a table seed).

For a product system I would avoid this type of convention as in my example any unique rowkey would suffice.

My first attempt was to use Count() as illustrated below:

return dataContext.CreateQuery(this.tableName)
                  .Where(n => n.PartitionKey == partitionKey).Count();

This caused a runtime DataServiceQueryException with the message of “An error occurred while processing this request.” Digging into the inner exception of DataServiceClientException showed a message of

  One of the request inputs is not valid.

I found an Azure Storage post: stating that Count() was currently not supported. So based on their helpful sample, the offending code was changed to:

                //return dataContext.CreateQuery(this.tableName).Where(n =&gt; n.PartitionKey == partitionKey).Count();
                var qry = dataContext.CreateQuery(this.tableName)
                                     .Where(n =&gt; n.PartitionKey == partitionKey);
                int count = 0;
                foreach (var row in qry)
                return count;

Hope this saves someone time!

This entry was posted in Azure, AzureStorage, C# and tagged , , . Bookmark the permalink.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>