AndrewPolland AndrewPolland - 3 months ago 26
C# Question

How do I query an Azure storage table with Linq?

I'm not sure where exactly, but I've got the wrong idea somewhere with this.

I'm trying to, in a first instance, query an azure storage table using linq. But I can't work out how it's done. From looking at a variety of sources I have the following:

List<BlogViewModel> blogs = new List<BlogViewModel>();

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("BlogConnectionString"));
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable blogTable = tableClient.GetTableReference("BlogEntries");

try
{
TableServiceContext tableServiceContext = tableClient.GetTableServiceContext();
TableServiceQuery<BlogEntry> query = (from blog in blogTable.CreateQuery<BlogEntry>()
select blog).AsTableServiceQuery<BlogEntry>(tableServiceContext);
foreach (BlogEntry blog in query)
{
blogs.Add(new BlogViewModel { Body = blog.Body });
}
}
catch { }


I probably had it closer before I messed around with it. Either that, or I'm misunderstanding what the TableService is. The following code did work for me, but I'm trying to switch it to using Linq instead.

List<BlogViewModel> blogs = new List<BlogViewModel>();

var storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("BlogConnectionString"));
var tableClient = storageAccount.CreateCloudTableClient();
CloudTable blogTable = tableClient.GetTableReference("BlogEntries");

TableRequestOptions reqOptions = new TableRequestOptions()
{
MaximumExecutionTime = TimeSpan.FromSeconds(1.5),
RetryPolicy = new LinearRetry(TimeSpan.FromSeconds(3), 3)
};
List<BlogEntry> lists;

try
{
var query = new TableQuery<BlogEntry>();
lists = blogTable.ExecuteQuery(query, reqOptions).ToList();

foreach (BlogEntry blog in lists)
{
blogs.Add(new BlogViewModel { Body = blog.Body });
}
}
catch { }


I've been unable to find a good solid example anywhere of what I should be doing. But from what I've been reading, it does suggest using Linq is possible. Any help or pointers appreciated. Thanks.




Slight update. The following is the syntax error I currently get on AsTableServiceQuery:

'System.Linq.IQueryable' does not contain a definition for 'AsTableServiceQuery' and no extension method 'AsTableServiceQuery' accepting a first argument of type 'System.Linq.IQueryable' could be found (are you missing a using directive or an assembly reference?)

However, I don't think this reflects the real issue, I think I have just got it put together wrong, just can't find a solid example anywhere that works.

Answer

TableServiceContext is no longer needed in the new Table Service Layer of Azure Storage Client Library. For more information on this change, please see our blog post Announcing Storage Client Library 2.1 RTM & CTP for Windows Phone.

Please make sure BlogEntry inherits from TableEntity and then the following code should work just fine:

List<BlogViewModel> blogs = new List<BlogViewModel>();

CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable blogTable = tableClient.GetTableReference("BlogEntries");

try
{
    IEnumerable<BlogEntry> query = (from blog in blogTable.CreateQuery<BlogEntry>()
                                    select blog);
    foreach (BlogEntry blog in query)
    {
        blogs.Add(new BlogViewModel { Body = blog.Body });
    }
}
catch { }
Comments