首页 > 其他 > 详细

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

时间:2017-02-10 14:37:46      阅读:270      评论:0      收藏:0      [点我收藏+]
 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         }

 

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

原文:http://www.cnblogs.com/DjangoBlogs/p/6386085.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!