How can I convert a .NET GUID to a MongoDB ObjectID (in C#). Also, can I convert it back again to the same GUID from the ObjectID?
FYI You can convert from an ObjectId to a Guid
public static Guid AsGuid(this ObjectId oid)
{
var bytes = oid.ToByteArray().Concat(new byte[] { 5, 5, 5, 5 }).ToArray();
Guid gid = new Guid(bytes);
return gid;
}
///
/// Only Use to convert a Guid that was once an ObjectId
///
public static ObjectId AsObjectId(this Guid gid)
{
var bytes = gid.ToByteArray().Take(12).ToArray();
var oid = new ObjectId(bytes);
return oid;
}