如何在 C# 中获取 Azure 表存储中的所有行? [英] How to get all rows in Azure table Storage in C#?

查看:20
本文介绍了如何在 C# 中获取 Azure 表存储中的所有行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取天蓝色表中所有实体的列表.

I am trying to get a list of all entities inside an azure table.

知道如何编写此查询吗?

Any idea of how I would write this query?

推荐答案

要回答您的问题,您可以执行以下操作:

To answer your question, you could do something like the following:

var acc = new CloudStorageAccount(
                         new StorageCredentials("account name", "account key"), true);
var tableClient = acc.CreateCloudTableClient();
var table = tableClient.GetTableReference("table name");
var entities = table.ExecuteQuery(new TableQuery<MyEntity>()).ToList();

但是请记住,表服务在一次调用中最多返回 1000 个实体.如果您的表中有超过 1000 个可用实体,它会返回一个 continuation token,可用于获取下一组实体.ExecuteQuery 方法实际上在内部处理这个延续令牌,因此如果你想因任何原因取消这个操作,你不能那样做.

However please keep in mind that table service returns a maximum of 1000 entities in a single call to it. If there're more than 1000 entities available in your table, it returns a continuation token which can be used to fetch next set of entities. The ExecuteQuery method actually handles this continuation token internally thus if you want to cancel this operation for any reason, you can't do that.

更好的方法是使用 ExecuteQuerySegmented 方法并让您的应用程序处理令牌.这是执行此操作的示例代码:

A better approach would be to use ExecuteQuerySegmented method and have your application deal with the token. Here's the sample code to do so:

var acc = new CloudStorageAccount(
                         new StorageCredentials("account name", "account key"), true);
var tableClient = acc.CreateCloudTableClient();
var table = tableClient.GetTableReference("table name");
TableContinuationToken token = null;
var entities = new List<MyEntity>();
do
{
    var queryResult = table.ExecuteQuerySegmented(new TableQuery<MyEntity>(), token);
    entities.AddRange(queryResult.Results);
    token = queryResult.ContinuationToken;
} while (token != null);

这篇关于如何在 C# 中获取 Azure 表存储中的所有行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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