简体   繁体   English

使用 .NET Core 从 Azure 表存储中检索前 n 条记录

[英]Retrieve top n records from Azure Table Storage with .NET Core

Is it possible to retrieve the Top n records from the Azure Table Storage using C#?是否可以使用 C# 从 Azure 表存储中检索前 n 条记录? I'm using .NET Core.我正在使用 .NET Core。

It would be great if I can get some references as well.如果我也能得到一些参考,那就太好了。

Please note that all my entities are stored using the Log Tail Pattern https://docs.microsoft.com/en-us/azure/cosmos-db/table-storage-design-guide#log-tail-pattern请注意,我所有的实体都是使用日志尾模式存储的https://docs.microsoft.com/en-us/azure/cosmos-db/table-storage-design-guide#log-tail-pattern

Thanks, Praveen谢谢,普拉文

You can use Take() method when creating your TableQuery :您可以在创建TableQuery时使用Take()方法:

TableQuery<T> query = new TableQuery<T>().Take(takeCount);

Or set the property TakeCount to specify it:或者设置属性TakeCount来指定它:

TableQuery<T> query = new TableQuery<T>();
query.TakeCount = takeCount;

What we did to get top entities of a table is as follows:我们为获取表的顶级实体所做的工作如下:

var query = new TableQuery<TEntity>()
            .Where(partitionFilter)
            .Take(limit);

return await ExecuteQuery(table, query);

Created the query and used .Take(limit) to specify how many entities we wanted.创建查询并使用.Take(limit)来指定我们想要的实体数量。

 do
 {
      var seg = await table.ExecuteQuerySegmentedAsync<TEntity>(query, token);

      token = seg.ContinuationToken;

      items.AddRange(seg);
 } while (token != null && items.Count < query.TakeCount);

In ExecuteQuery method we counted how many records were retrieved from the table.ExecuteQuery方法中,我们计算从表中检索了多少记录。 This is how we did it and worked, I assume that in order to take top n entities the limit should be set to n .这就是我们这样做和工作的方式,我假设为了获取前n实体, limit应设置为n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM