首页 > 代码库 > 比较两个泛型列表,取得不同项

比较两个泛型列表,取得不同项

 1         public static List<T> GetModiflyList<T>(List<T> newList, List<T> oldList, string keyField)
 2         {
 3             List<T> list = new List<T>();
 4             foreach (T newModel in newList)
 5             {
 6                 object nob = newModel.GetType().GetProperty(keyField).GetValue(newModel, null);
 7                 T oldmodel = oldList.Find((delegate (T old) { object ob = old.GetType().GetProperty(keyField).GetValue(old, null); if (object.Equals(ob, nob)) return true; else return false; }));
 8                 if (oldmodel == null)
 9                 {
10                     list.Add(newModel);
11                 }
12                 else
13                 {
14                     PropertyInfo[] pi = oldmodel.GetType().GetProperties();
15                     foreach (PropertyInfo p in pi)
16                     {
17                         object o_new = p.GetValue(newModel, null);
18                         object o_old = p.GetValue(oldmodel, null);
19                         if (object.Equals(o_new, o_old))
20                             continue;
21                         else
22                         {
23                             list.Add(newModel);
24                             break;
25                         }
26                     }
27                 }
28             }
29 
30             return list;
31         }

 

比较两个泛型列表,取得不同项