码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-02-10 14:38:27      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:equal   ati   两个泛型   tty   ret   code   log   mode   lis   

 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         }

 

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

标签:equal   ati   两个泛型   tty   ret   code   log   mode   lis   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!