我不确定究竟在哪里,但我在这个地方有错误的想法。如何使用Linq查询Azure存储表?
我试图在第一个实例中使用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 { }
在我弄乱它之前,我可能已经更接近它了。要么,要么我误解了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 { }
我一直无法在我应该做的任何地方找到一个很好的实例。但从我一直在阅读的内容来看,它确实表明使用Linq是可能的。任何帮助或指针赞赏。谢谢。
轻微更新。以下是我当前在AsTableServiceQuery上得到的语法错误:
'System.Linq.IQueryable'不包含'AsTableServiceQuery'的定义,也没有接受'System.Linq'类型的第一个参数的扩展方法'AsTableServiceQuery'。 IQueryable“可以找到(你是否缺少使用指令或程序集引用?)
但是,我不认为这反映了真正的问题,我认为我刚刚把它放在一起错了,只是不能在任何地方找到一个可靠的例子。
究竟是怎么回事?你是否收到错误消息?没有数据? – Igorek
@Igorek对不起,我已更新错误。但是,我不认为代码足够接近错误的真正含义。 – AndrewPolland