Can be used when you want to generat unique ID in your domain model.
public Guid NewSequentialID()
{
var destinationArray = Guid.NewGuid().ToByteArray();
var time = new DateTime(0x76c, 1, 1);
var now = DateTime.Now;
var span = new TimeSpan(now.Ticks - time.Ticks);
var timeOfDay = now.TimeOfDay;
var bytes = BitConverter.GetBytes(span.Days);
var array = BitConverter.GetBytes((long) (timeOfDay.TotalMilliseconds / 3.333333));
Array.Reverse(bytes);
Array.Reverse(array);
Array.Copy(bytes, bytes.Length - 2, destinationArray, destinationArray.Length - 6, 2);
Array.Copy(array, array.Length - 4, destinationArray, destinationArray.Length - 4, 4);
return new Guid(destinationArray);
}
Note: Code is from Doug posted on StackOverflow: Sequential guid in linq to sql
Fork
0 Feedback
You must log in before you can give any feedback
You must log in before you can post a comment


468
0




Mark 'c#' tag as 'like'
Mark 'c#' tag as 'ignore'