首页 > 代码库 > Mongo:将查询结果转换为自定义类

Mongo:将查询结果转换为自定义类

1、自定义类

    public class MyClass
    {
        public string Name { get; set; }
        public int Corners { get; set; }
    }

 

2、查询数据并转换

   var config = new MongoConfigurationBuilder();
            config.ConnectionString("");
            using (Mongo mongo = new Mongo(config.BuildConfiguration()))
            {
                mongo.Connect();
                try
                {
                    var db = mongo.GetDatabase("MyTest");
                    var list = db.GetCollection<MyClass>("MyClass").Linq().ToList<MyClass>();

                    var superclass =
                        db.GetCollection<MyClass>("MyClass").Linq().Where(p => p.Corners > 1).ToList<MyClass>();
                }
                finally
                {
                    mongo.Disconnect();
                }
            }