zoukankan      html  css  js  c++  java
  • 对象比对

     var classA = new List<FatherClass>
                                 {
                                     new FatherClass {
                                             ID = "1",
                                             Name = "1",
                                             Other =
                                                 new List<ClassOne>
                                                     {
                                                         new ClassOne
                                                             {
                                                                 ID = 11,
                                                                 Professional
                                                                     =
                                                                     "xy1",
                                                                 Post =
                                                                     "xy1"
                                                             },
                                                         new ClassOne
                                                             {
                                                                 ID = 12,
                                                                 Professional
                                                                     =
                                                                     "wy1",
                                                                 Post =
                                                                     "wy1"
                                                             }
                                                     }
                                         }
                                 };
    
                var classB = new List<FatherClass>
                                 {
                                     new FatherClass {
                                             ID = "1",
                                             Name = "1",
                                             Other =
                                                 new List<ClassOne>
                                                     {
                                                         new ClassOne
                                                             {
                                                                 Professional
                                                                     =
                                                                     "xy1",
                                                                 Post
                                                                     =
                                                                     "xy12"
                                                             },
                                                         new ClassOne
                                                             {
                                                                 Professional
                                                                     =
                                                                     "wy1",
                                                                 Post
                                                                     =
                                                                     "wy13"
                                                             }
                                                     }
                                         },
                                     new FatherClass {
                                             ID = "2",
                                             Name = "2",
                                             Other =
                                                 new List<ClassOne>
                                                     {
                                                         new ClassOne
                                                             {
                                                                 Professional
                                                                     =
                                                                     "xy2",
                                                                 Post
                                                                     =
                                                                     "xy2"
                                                             },
                                                         new ClassOne
                                                             {
                                                                 Professional
                                                                     =
                                                                     "wy2",
                                                                 Post
                                                                     =
                                                                     "wy2"
                                                             }
                                                     }
                                         }
                                 };
    
                // 多余的外部订单集合
                List<FatherClass> excessSellRefundList = null;
    
                // 筛选多余的外部订单集合对象
                foreach (var outSellRefund in classB)
                {
                    var sellRefundTs = from optimSell in classA
                                       where
                                           optimSell.Other.Find(
                                               arg =>
                                               optimSell.Name.Equals(outSellRefund.Name)
                                               && outSellRefund.Other.Find(xy => arg.Professional.Equals(xy.Professional))
                                               != null) == null
                                       select optimSell;
    
                    if (!sellRefundTs.Any())
                    {
                        continue;
                    }
    
                    if (excessSellRefundList == null)
                    {
                        excessSellRefundList = new List<FatherClass>();
                    }
    
                    excessSellRefundList.Add(outSellRefund);
                }
    
                // 相同的ID对象内部指定参数赋值
                foreach (var a in classA)
                {
                    // 找到相同名字的参数对象(从classB中找到classA相对应名字的对象)
                    var model = classB.Find(arg => arg.Name.Equals(a.Name));
    
                    if (model == null)
                    {
                        continue;
                    }
    
                    // 比对对象中的对应属性值
                    foreach (var property in a.GetType().GetProperties())
                    {
                        // 排除主键
                        if (property.Name.Equals("ID"))
                        {
                            continue;
                        }
    
                        // 循环model对象中的对应属性值
                        foreach (var outProperty in model.GetType().GetProperties())
                        {
                            // 排除主键
                            if (outProperty.Name.Equals("ID"))
                            {
                                continue;
                            }
    
                            // 判断属性名是否相同
                            if (property.Name.Equals(outProperty.Name))
                            {
                                // 判断当前属性类型是否为泛型集合
                                if (property.PropertyType == typeof(List<ClassOne>))
                                {
                                    // 得到对应的属性值
                                    var tmpClassOne = property.GetValue(a, null) as List<ClassOne>;
    
                                    // 得到对应的属性值
                                    var tmpOutClassOne = outProperty.GetValue(model, null) as List<ClassOne>;
    
                                    if (tmpClassOne == null || tmpClassOne.Count == 0)
                                    {
                                        continue;
                                    }
    
                                    if (tmpOutClassOne == null || tmpOutClassOne.Count == 0)
                                    {
                                        continue;
                                    }
    
                                    // 循环泛型集合对象
                                    foreach (var fatherClass in tmpClassOne)
                                    {
                                        // 循环需要比对的泛型集合对象
                                        foreach (var classOne in tmpOutClassOne)
                                        {
                                            // 比对关键值是否相同
                                            if (fatherClass.Professional.Equals(classOne.Professional))
                                            {
                                                // 进行属性赋值(循环)
                                                foreach (var propertyInfo in fatherClass.GetType().GetProperties())
                                                {
                                                    // 排除指定ID
                                                    if (propertyInfo.Name.Equals("ID"))
                                                    {
                                                        continue;
                                                    }
    
                                                    // 循环需要比对的对象的属性
                                                    foreach (var onePropertyInfo in classOne.GetType().GetProperties())
                                                    {
                                                        if (onePropertyInfo.Name.Equals("ID"))
                                                        {
                                                            continue;
                                                        }
    
                                                        // 属性名相同,进行赋值操作
                                                        if (propertyInfo.Name.Equals(onePropertyInfo.Name))
                                                        {
                                                            propertyInfo.SetValue(
                                                                fatherClass,
                                                                onePropertyInfo.GetValue(classOne, null),
                                                                null);
                                                        }
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                break;
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    // 属性名相同,进行赋值操作
                                    if (property.Name.Equals(outProperty.Name))
                                    {
                                        property.SetValue(a, outProperty.GetValue(model, null), null);
                                    }
                                }
                            }
                        }
                    }
                }
    
                if (excessSellRefundList != null && excessSellRefundList.Count > 0)
                {
                    classA.AddRange(excessSellRefundList);
                }
    /// <summary>
        /// @class类 
        /// </summary>
        [Serializable]
        public class ClassOne
        {
            public int ID { get; set; }
    
            public string Professional { get; set; }
    
            public string Post  { get; set; }
        }
    
        /// <summary>
        /// FatherClass类 
        /// </summary>
        [Serializable]
        public class FatherClass
        {
            public string ID { get; set; }
    
            public string Name { get; set; }
    
            public List<ClassOne> Other { get; set; }
        }
    
  • 相关阅读:
    富文本ZSSRichTextEditor之趟坑集锦
    iOS11及Xcode9适配问题汇总
    XIB中拖UIScrollView的困难
    Glide加载图片问题记录
    微信关注事件bug记录
    kswapd0 进程 设置 swap
    AJAX请求返回HTTP 400 错误
    通过jQuery Ajax提交表单数据时同时上传附件
    JS---DOM---事件冒泡和阻止事件冒泡,总结事件
    JS---DOM---为元素绑定事件和解绑事件的兼容代码
  • 原文地址:https://www.cnblogs.com/NoRoad/p/3482424.html
Copyright © 2011-2022 走看看