发布网友 发布时间:2022-04-24 16:48
共2个回答
懂视网 时间:2022-04-08 01:23
一 从超级变态的类开始
public class Person : Base { public Person() { Contact = new Test.Contact(); OrderList = new List<Order>(); } public string Name { get; set; } public DateTime LastContact { get; set; } public DateTime Birthday { get; set; } public int Age { get; set; } #region 值对象 /// <summary> /// 统计 /// </summary> public Total Total { get; set; } /// <summary> /// 联系方式和地址 /// </summary> public Contact Contact { get; set; } #endregion #region 列表实体 public List<Order> OrderList { get; set; } #endregion } public class Section { public string SectionID { get; set; } public string SectionName { get; set; } } public class Area { public Area() { Section = new Section(); } public string Province { get; set; } public string City { get; set; } public string District { get; set; } public Section Section { get; set; } } public class Contact { public Contact() { Area = new Area(); } public string PostCode { get; set; } public string Email { get; set; } public string Phone { get; set; } public Area Area { get; set; } } public class Total { public int Count { get; set; } public int Max { get; set; } } public class Order { public Order() { Id = MongoDB.Bson.ObjectId.GenerateNewId().ToString(); this.OrderDetail = new List<OrderDetail>(); this.User_Info = new User_Info(); } public string UserId { get; set; } public string UserName { get; set; } public string Id { get; set; } public double Price { get; set; } public DateTime AddTime { get; set; } public User_Info User_Info { get; set; } public List<OrderDetail> OrderDetail { get; set; } } public class User_Info { public User_Info() { Id = MongoDB.Bson.ObjectId.GenerateNewId().ToString(); } public string Id { get; set; } public string Name { get; set; } } public class OrderDetail { public OrderDetail() { Id = MongoDB.Bson.ObjectId.GenerateNewId().ToString(); } public string Id { get; set; } public string OrderId { get; set; } public string ProductName { get; set; } public int Count { get; set; } public double Price { get; set; } public string SellerId { get; set; } }
看到上面的类,绝对够你喝一壶的,呵呵,这是一个复杂的类型People,它有实体属性contact和列表属性OrderList
而对于之前大叔的框架里,这种结构是不被支持的,大叔只能支持到3级嵌套,但这显然是不够的,最后大叔硬着头皮冲了上来,把这个骨头啃掉了,哈哈!
下面贡献我的Recursion代码
/// <summary> /// 递归构建Update操作串 /// </summary> /// <param name="fieldList"></param> /// <param name="property"></param> /// <param name="propertyValue"></param> /// <param name="item"></param> /// <param name="father"></param> private void GenerateRecursion( List<UpdateDefinition<TEntity>> fieldList, PropertyInfo property, object propertyValue, TEntity item, string father) { //复杂类型 if (property.PropertyType.IsClass && property.PropertyType != typeof(string) && propertyValue != null) { //集合 if (typeof(IList).IsAssignableFrom(propertyValue.GetType())) { foreach (var sub in property.PropertyType.GetProperties(BindingFlags.Instance | BindingFlags.Public)) { if (sub.PropertyType.IsClass && sub.PropertyType != typeof(string)) { var arr = propertyValue as IList; if (arr != null && arr.Count > 0) { for (int index = 0; index < arr.Count; index++) { foreach (var subInner in sub.PropertyType.GetProperties(BindingFlags.Instance | BindingFlags.Public)) { if (string.IsNullOrWhiteSpace(father)) GenerateRecursion(fieldList, subInner, subInner.GetValue(arr[index]), item, property.Name + "." + index); else GenerateRecursion(fieldList, subInner, subInner.GetValue(arr[index]), item, father + "." + property.Name + "." + index); } } } } } } //实体 else { foreach (var sub in property.PropertyType.GetProperties(BindingFlags.Instance | BindingFlags.Public)) { if (string.IsNullOrWhiteSpace(father)) GenerateRecursion(fieldList, sub, sub.GetValue(propertyValue), item, property.Name); else GenerateRecursion(fieldList, sub, sub.GetValue(propertyValue), item, father + "." + property.Name); } } } //简单类型 else { if (property.Name != EntityKey)//更新集中不能有实体键_id { if (string.IsNullOrWhiteSpace(father)) fieldList.Add(Builders<TEntity>.Update.Set(property.Name, propertyValue)); else fieldList.Add(Builders<TEntity>.Update.Set(father + "." + property.Name, propertyValue)); } } } /// <summary> /// 构建Mongo的更新表达式 /// </summary> /// <param name="entity"></param> /// <returns></returns> private List<UpdateDefinition<TEntity>> GeneratorMongoUpdate(TEntity item) { var fieldList = new List<UpdateDefinition<TEntity>>(); foreach (var property in typeof(TEntity).GetProperties(BindingFlags.Instance | BindingFlags.Public)) { GenerateRecursion(fieldList, property, property.GetValue(item), item, string.Empty); } return fieldList; }
最后的结果,当然是在N层失败之后,取得了成功,呵呵!
热心网友 时间:2022-04-07 22:31
虽然我们不相识,但你仍然毫不犹豫地向我伸来援助之手。这件事,虽不惊天动地,却感动了我。
那一天,放学晚了,我急匆匆地背上书包,就往教室外冲。刚冲出教室,就傻了眼了:下大雨了。黄豆大小的雨珠密密麻麻地落在学校操场上,溅起了水花。我摸了摸书包两侧:糟了,下雨天,我竟然没有带伞!爸爸妈妈今天又不会来接我,我该怎么办?无奈之下,我只好硬着头皮冲出教学楼。
到了路口,我随手拦了一辆三轮车,向骑车人说了目的地,就什么也不管了。
雨越下越大,天也渐渐暗了下来。骑车人双脚用力蹬着脚踏板,路上不时地溅起水花。我上上下下、仔仔细细地打量了他一番:黑黝黝的脸上布满了皱纹;身穿蓝色的T恤衫,说是蓝色,其实已经变黑;黑色的九分裤;脚上穿着旧布鞋;肩上搭着一条毛巾。怎么看,都像是一个饱经风霜的中年人。
三轮车仍然冒着大雨匍匐前进着……
到了家门口,我跳下车,准备付钱。可我搜遍了书包上所有的袋子,衣服上大大小小的口袋,也只找出五角钱。我急得如同热锅上的蚂蚁——团团转,心想:老天,别再下雨了,快快掉四块钱下来吧!
骑车人似乎看出了我的心思,操着浓重的方言说:“算了算了,不就是四块钱嘛,免了免了。”骑车人的这番话,如同让我抓住了救命草,但是,心里却想:下雨天,总不能让人家白跑一趟,空着手回去吧,人家赚些钱也不容易。
等我回过神来,三轮车早骑远了,渐渐消失在雨帘中,手中的那枚五角钱硬币被我捏得紧紧的。此时此刻,我的心情如同这枚硬币,滚烫滚烫,上下翻腾,我在心底里默默得祝福:好人一生平安!