How to retrieve latest record using RowKey or Timestamp in Azure Table storage
Neo,
If you need to have the latest entry in your partition, using a string date time for the row key is not a good approach as Table Storage stores entities in ascending order based on the Row Key.
If at the current point you can change the value of your row key, use DateTime.UtcNow.Ticks
:
var invertedTimeKey = DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks
With that approach, when querying your table, you will be able to take 1 entry corresponding to the latest.
If you can't change the value of your row key, you will have to retrieve all the entries in the partition, meaning loading all of it in memory, and then order them using the Timestamp to retrieve the last one. If you have a lot of entries this is definitely not a good approach.
var lastResult = results.OrderByDescending(r => r.Timestamp).FirstOrDefault();
Azure Table Service doesn't support Order By
functionality, thus with the current setup only option for you is to download all entities and sort them reverse chrnologically on the client side. This obviously is not an optimal solution when the number of entities in the table become large.
Other option (which would require you to redesign the application) would be to convert the date/time value in reverse ticks:
var rowKey = (DateTime.MaxValue.Ticks - DateTimeValueForRowKey.Ticks).ToString("d19")
This will ensure that the latest entries are added to the top of the table instead of at the bottom of the table. To fetch the latest entry, you would just have to take 1st entity from the table.