如何查询使用LINQ的Azure存储表? [英] How do I query an Azure storage table with Linq?

查看:357
本文介绍了如何查询使用LINQ的Azure存储表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道究竟在何处,但我已经对这个有错误的想法的地方。

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

我想,在一审,查询使用LINQ的Azure存储表。但它是如何做我不能工作了。通过观察各种来源我有以下几点:

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 { }

我可能有更接近之前,我与它搞砸左右。要么,我误解了TableService是什么。下面code为我所做的工作,但我想将它转换为使用LINQ来代替。

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 { }

我已经无法对什么我应该做的任何地方找到一个很好的实训。但是从我一直在读什么,它确实表明使用LINQ是可能的。任何帮助或指针AP preciated。谢谢你。

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.

轻微的更新。以下是语法错误我目前得到AsTableServiceQuery:

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

System.Linq.IQueryable'不包含'AsTableServiceQuery',没有扩展方法'AsTableServiceQuery接受型System.Linq.IQueryable'的第一个参数的定义可以找到(是否缺少using指令或程序集引用?)

'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.

推荐答案

TableServiceContext不再需要Azure存储客户端库的新表服务层英寸有关此更改的详细信息,请参阅我们的博客文章<一个href=\"http://blogs.msdn.com/b/windowsazurestorage/archive/2013/09/07/announcing-storage-client-library-2-1-rtm.aspx\">Announcing存储客户端库2.1 RTM和放大器; CTP Windows Phone的。

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.

请确保BlogEntry从<一个继承href=\"http://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.table.tableentity%28v=azure.10%29.aspx\">TableEntity然后将下面的code应该只是罚款:

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 { }

这篇关于如何查询使用LINQ的Azure存储表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆