Skip to content

Instantly share code, notes, and snippets.

@panicoenlaxbox
Created May 20, 2014 08:55
Show Gist options
  • Save panicoenlaxbox/5c6784e2be1171eed003 to your computer and use it in GitHub Desktop.
Save panicoenlaxbox/5c6784e2be1171eed003 to your computer and use it in GitHub Desktop.
MongoCSharpDriver deserialize anonymous type
public class TempDataDocument
{
public string Id { get; set; }
public string Key { get; set; }
public object Value { get; set; } // Aquí puede ir cualquier cosa
}
public static void Main()
{
var client = new MongoClient("mongodb://localhost:27017");
var server = client.GetServer();
var database = server.GetDatabase("tests");
var collection = database.GetCollection("anonymous");
var document = new TempDataDocument()
{
Key = "key1",
Value = new { Nombre = "Sergio", Twitter = "@panicoenlaxbox" } // Guardamos un tipo anónimo
};
collection.Insert(document);
var data = collection.FindAs<TempDataDocument>(Query.EQ("Key", "key1"));
/* An unhandled exception of type 'System.IO.FileFormatException' occurred in MongoDB.Driver.dll
Additional information: An error occurred while deserializing the Value property of class MongoDb1.TempDataDocument: A document being deserialized to System.Object must be empty.*/
Console.WriteLine(data.ToList().First().Value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment