如何在C#中获取Azure表存储中的所有行? [英] How to get all rows in Azure table Storage in 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屋!