比较两个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;
        }

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

posted @ 2017-02-10 13:56  Max-Jiang  阅读(736)  评论(0编辑  收藏  举报