简体   繁体   中英

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#? I'm using .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

Thanks, Praveen

You can use Take() method when creating your TableQuery :

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

Or set the property TakeCount to specify it:

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.

 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. 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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