Skip to content

Instantly share code, notes, and snippets.

@sacarino
Created June 28, 2017 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sacarino/a7869b8ef4917c75623a35fb74243cc2 to your computer and use it in GitHub Desktop.
Save sacarino/a7869b8ef4917c75623a35fb74243cc2 to your computer and use it in GitHub Desktop.
C# - Generate a GUID from an ObjectID
using System;
using System.Linq;
using MongoDB.Bson;
namespace Extensions
{
internal static class BsonToGuid
{
internal static Guid AsGuid(this BsonObjectId oid)
{
var bytes = oid.Value.ToByteArray().Concat(new byte[] { 5, 5, 5, 5 }).ToArray();
Guid gid = new Guid(bytes);
return gid;
}
}
}
@gmarbury
Copy link

Why 5s?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment