如何使用Linq查询Azure存储表?

And*_*and 16 c# linq azure azure-storage azure-table-storage

我不确定到底在哪里,但我对此有错误的想法.

我试图在第一个实例中使用linq查询azure存储表.但我无法弄清楚它是如何完成的.从各种来源看,我有以下几点:

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 { }
Run Code Online (Sandbox Code Playgroud)

在我搞砸之前,我可能已经把它靠近了.要不然,或者我误解了TableService是什么.以下代码对我有用,但我试图将其切换为使用Linq.

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 { }
Run Code Online (Sandbox Code Playgroud)

我无法在任何我应该做的事情上找到一个很好的例子.但是从我一直在阅读的内容来看,确实建议使用Linq是可能的.任何帮助或指针赞赏.谢谢.


稍微更新.以下是我目前在AsTableServiceQuery上获得的语法错误:

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

但是,我不认为这反映了真正的问题,我想我刚刚将它放在一起是错误的,只是找不到任何有效的实例.

Ser*_*ler 26

Azure存储客户端库的新表服务层中不再需要TableServiceContext.有关此更改的更多信息,请参阅我们的博客文章Announcing Storage Client Library 2.1 RTM和CTP for Windows Phone.

请确保BlogEntry实现ITableEntity,然后以下代码应该可以正常工作:

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 { }
Run Code Online (Sandbox Code Playgroud)

  • 我相信这个答案现在已经过时了:尝试执行以下操作:IEnumerable &lt;Translation&gt; query =(来自table.CreateQuery &lt;Translation&gt;()select blog中的博客);并在select select blog上获得此异常*无法转换查询键入ilist &lt;string&gt; *的表达式 (2认同)

Bre*_*een 15

我当前的表存储库执行此操作:

public IQueryable<TEntity> Find(Expression<Func<TEntity, bool>> expression) 
{
    if (IsTableEmpty())
    {
        return Enumerable.Empty<TEntity>().AsQueryable();
    }
    else
    {
        return _cloudTable.CreateQuery<TEntity>().AsQueryable().Where(expression);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的_ cloudTable对应你的blogTable.