/TableStorageKeys.cs Secret
Created
April 5, 2022 17:23
Convert DateTime.Now to an ever-decreasing Ticks String to be used as Keys in Azure Table Storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Returns an ever-decreasing number as string to be used as PartitionKey or RowKey. | |
/// Ensures that newly created entities are always "on top" and returned first in queries | |
/// </summary> | |
/// <returns></returns> | |
public static string TicksKey() | |
{ | |
return (DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks).ToString("d19"); | |
} | |
/// <summary> | |
/// Converts TicksKey back to DateTimeOffset | |
/// </summary> | |
/// <param name="ticksKey"></param> | |
/// <returns></returns> | |
public static DateTimeOffset TicksKeyToDateTimeOffset(string ticksKey) | |
{ | |
return new DateTimeOffset(DateTime.MaxValue.Ticks - long.Parse(ticksKey), TimeSpan.Zero); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment