MongoDB存储泛型List<User>集合

 protected virtual MongoConfigurationBuilder GetConfiguration()

创新互联凭借专业的设计团队扎实的技术支持、优质高效的服务意识和丰厚的资源优势,提供专业的网站策划、成都网站建设、做网站、网站优化、软件开发、网站改版等服务,在成都10余年的网站建设设计经验,为成都上1000+中小型企业策划设计了网站。

        {
            var builder = new MongoConfigurationBuilder();
            builder.ReadConnectionStringFromAppSettings("tests");
            return builder;
        }
        //数据映射类(key,value)
        class ListInfo
        {
            public int key { get; set; }
            public object value { get; set; }
        }
        static void Main(string[] args)
        {
            var config = new MongoConfigurationBuilder();
            // COMMENT OUT FROM HERE
            config.Mapping(mapping =>
            {
                mapping.DefaultProfile(profile =>
                {
                    profile.SubClassesAre(t => t.IsSubclassOf(typeof(ListInfo)));
                });
                mapping.Map();
            });

            config.ConnectionString("Server=127.0.0.1");
            using (Mongo mongo = new Mongo(config.BuildConfiguration()))
            {
                mongo.Disconnect();
                mongo.Connect();
                try
                {
                    var db = mongo.GetDatabase("TestDb");
                    var collection = db.GetCollection();
                    //添加信息 需要就可以打开注释
                    ListInfo list = BindVendor();
                    collection.Save(list);
                    var coll = db.GetCollection("ListInfo");
                    var info = coll.Find(new Document().Add("key", 7888)).Documents.ToList();
                    string showinfo = "";
                    foreach (Document item in info)
                    {

                        var listd = item.Values.ToList();
                        showinfo += "编号:" + listd[1];
                        List items = (listd[2] as List);
                        foreach (var item1 in items)
                        {
                            var item2 = item1.ToList()[1].Value;
                            showinfo += "名称:" + item2;
                        }

                    }
                    Console.WriteLine(showinfo);
                }
                catch { }
            }
            Console.ReadKey();
        }

        #region 集合信息

        #region 添加信息到结合
        private static ListInfo BindVendor()
        {
            vendor square = new vendor()
            {
                vendorid = 2,
                vendorname = "西郊汽配城",
                itemid = DateTime.Now.Second
            };
            List list = new List();
            list.Add(square);
            ListInfo listinfo = new ListInfo()
            {
                key = 7888,
                value = list
            };
            return listinfo;
        }
        #endregion
        #region 实体类
        class vendor
        {
            public int vendorid { get; set; }
            public string vendorname { get; set; }
            public int itemid { get; set; }
        }
        #endregion

        #endregion


当前题目:MongoDB存储泛型List<User>集合
浏览路径:http://bzwzjz.com/article/jccdgj.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都网站设计 成都网站设计 成都网站设计 手机网站设计 成都网站设计 手机网站建设套餐 成都网站建设 成都网站建设 企业网站建设公司 成都网站建设 成都网站制作 成都网站建设 成都定制网站建设 营销网站建设 网站建设方案 成都网站建设 成都网站建设公司 外贸营销网站建设 自适应网站设计 成都网站建设 成都网站制作 营销型网站建设