Skip to content

Instantly share code, notes, and snippets.

@azkurban
Forked from sacarino/BsonToGuid.cs
Created November 3, 2022 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azkurban/74f45aa7305195c841fa4be44199aff8 to your computer and use it in GitHub Desktop.
Save azkurban/74f45aa7305195c841fa4be44199aff8 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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment