6
votes

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

2
Please include the code for how you (would) do it without the Top N condition. If you use Linq, see if adding .Take(n) works.Peter B

2 Answers

11
votes

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;
0
votes

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.