using System; using System.Text; using System.Collections.Generic; using System.Linq; using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoDB.Driver; using MongoDB.Bson; using MongoDB.Driver.Linq; namespace Root.Common.Tests.MongoDB.MongoTests { [TestClass] public class MongoTests { public interface IEntity { string Name { get; set; } } public class Entity : IEntity { public Entity() { } public string Name { get; set; } } [TestMethod] public void TestMethod2() { MongoServer server = MongoServer.Create(string.Format("mongodb://localhost")); Assert.IsNotNull(server); MongoDatabase database = server.GetDatabase("MongoTests"); Assert.IsNotNull(database); database.Drop(); MongoCollection collection = database.GetCollection("Entity"); Assert.IsNotNull(collection); for (int i = 0; i < 100; ++i) { Entity e = new Entity() { Name = "Name_" + i }; WriteConcernResult result = collection.Insert(e.GetType(), e, WriteConcern.Acknowledged); Assert.IsTrue(result.Ok); } int count = (from e in collection.AsQueryable() where true || e.Name == "Name_22" select e).Count(); Assert.IsTrue(count == 100); } [TestMethod] public void TestMethod1() { MongoServer server = MongoServer.Create(string.Format("mongodb://localhost")); Assert.IsNotNull(server); MongoDatabase database = server.GetDatabase("MongoTests"); Assert.IsNotNull(database); database.Drop(); MongoCollection collection = database.GetCollection("Entity"); Assert.IsNotNull(collection); for (int i = 0; i < 100; ++i) { Entity e = new Entity() { Name = "Name_" + i }; WriteConcernResult result = collection.Insert(e.GetType(), e, WriteConcern.Acknowledged); Assert.IsTrue(result.Ok); } IQueryable intermediateQuery = from e in collection.AsQueryable() select e; //works int count = intermediateQuery.Count(); IQueryable finalQuery = from e in intermediateQuery where e.Name == "Name_22" select e; //Throws Exception (can't find serializer for IEntity)??? var entity = finalQuery.FirstOrDefault(); } } }