zoukankan      html  css  js  c++  java
  • 比较两个List列表,取得List中不同项返回

            /// <summary>
            /// 比对模型及属性数组
            /// </summary>
            /// <typeparam name="TM"></typeparam>
            /// <param name="newList"></param>
            /// <param name="oldList"></param>
            /// <param name="keyField">模型Id</param>
            /// <param name="keyArrField">模型可转换数组</param>
            /// <returns></returns>
            public static List<TM> CompareList<TM>(List<TM> newList, List<TM> oldList, string keyField,
                string[] keyArrField)
            {
                List<TM> list = new List<TM>();
    
                foreach (TM newModel in newList)
                {
                    object nob = newModel.GetType().GetProperty(keyField).GetValue(newModel, null);
                    TM oldmodel = oldList.Find((delegate(TM old)
                    {
                        object ob = old.GetType().GetProperty(keyField).GetValue(old, null);
                        if (object.Equals(ob, nob)) return true;
                        else return false;
                    }));
                    if (oldmodel == null)
                    {
                        list.Add(newModel);
                    }
                    else
                    {
                        PropertyInfo[] pi = oldmodel.GetType().GetProperties();
                        foreach (PropertyInfo p in pi)
                        {
                            object o_new = p.GetValue(newModel, null);
                            object o_old = p.GetValue(oldmodel, null);
    
                            if (keyArrField?.Contains(p.Name) ?? false)
                            {
                                if (o_new.IsNotNull() && o_old.IsNotNull())
                                {
                                    var o_old_list = o_old.ToString().SplitList();
                                    var o_new_list = o_new.ToString().SplitList();
                                    if (o_old_list.Except(o_new_list).Any() ||
                                        o_new_list.Except(o_old_list).Any())
                                    {
                                        list.Add(newModel);
                                        break;
                                    }
                                    else
                                        continue;
                                }
                                else if ((o_new == null || o_new.ToString() == string.Empty) && (o_old == null || o_old.ToString() == string.Empty))
                                {
                                    continue;
                                }
                                else
                                {
                                    list.Add(newModel);
                                    break;
                                }
                            }
                            else
                            {
                                if (object.Equals(o_new, o_old))
                                    continue;
                                else
                                {
                                    list.Add(newModel);
                                    break;
                                }
                            }
    
                        }
                    }
                }
    
                return list;
            }
    

      做版本时候,需要对很多实体做比对,写了这个小工具。

  • 相关阅读:
    《剑指Offer》二维数组中的查找
    白话计算机入门书籍--《穿越计算机的迷雾》有感
    Mysql Cluster7.5.6在 windows10 部署安装
    Mysql Cluster7.5.6 windows10 部署安装
    lll
    线程控制
    动态链接库相关知识
    二分查找及其变种简单易懂的模版
    白话 STL next_permutation 原理
    Maven本地上有包还去网上找包
  • 原文地址:https://www.cnblogs.com/DjangoBlogs/p/6386085.html
Copyright © 2011-2022 走看看