gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:04:20 28 4
gpt4 key购买 nike

我不确定具体在哪里,但我在某个地方有错误的想法。

我首先尝试使用 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.IQueryable”类型的第一个参数的扩展方法“AsTableServiceQuery”(您是否缺少 using 指令或程序集引用?)

但是,我不认为这反射(reflect)了真正的问题,我认为我只是把它组合在一起是错误的,只是找不到任何可行的可靠示例。

最佳答案

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

关于c# - 如何使用 Linq 查询 Azure 存储表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23392053/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com