随笔-63  评论-48  文章-2 

对比两个同类型的List返回差异List集合

原创,转载请注明出处

http://www.cnblogs.com/lhxhappy/archive/2009/01/05/1368635.html

 /// <summary>
        
/// 对比两个同类型的List<T>返回差异List<T>集合
        
/// Created by lhxhappy
        
/// 2009-01-05 01:08:30
        
/// </summary>
        
/// <typeparam name="T">泛型类型</typeparam>
        
/// <param name="newModel">修改后的数据集合</param>
        
/// <param name="oldModel">原始数据集合</param>
        
/// <returns>返回与原始集合有差异的集合</returns>
        private List<T> GetModiflyList<T>(List<T> newModel, List<T> oldModel)
        {
            List
<T> list = new List<T>();
            
foreach (T newMod in newModel)
            {
                
bool IsExist = false;
                
foreach (T oldMol in oldModel)
                {
                    
//取得老实体对象的属性集合
                    PropertyInfo[] pi = oldMol.GetType().GetProperties();
                    
//定义记数器
                    int i = 0;

                    
//将老实体对象的没一个属性值和新实体对象进行循环比较
                    foreach (PropertyInfo p in pi)
                    {
                        
//防止object.Equals时实例化对象发生异常
                        object o_new = newMod.GetType().GetProperty(p.Name).GetValue(newMod, null);
                        
if (o_new == null)
                            o_new 
= (object)String.Empty;

                        
//防止object.Equals时实例化对象发生异常
                        object o_old = p.GetValue(oldMol, null);
                        
if(o_old == null)
                            o_old 
= (object)String.Empty;

                        
//新老实体比较并记录成功次数
                        if (object.Equals(o_new, o_old))
                        {
                            i
++;
                        }
                        
//若成功次数和属性数目相等则说明已经存在或者没有发生过修改条出循环
                        if (i == pi.Length)
                        {
                            IsExist 
= true;
                            
break;
                        }
                    }

                    
//没有发生过修改条出循环
                    if (IsExist)
                        
break;
                }

                
//如果不存在则添加该实体到List<T>中
                if (!IsExist)
                    list.Add(newMod);

            }
            
return list;
        }

 

根据数据主键对比进行差异结果的返回,可节省60%的循环次数

 

/// <summary>
        
/// 对比两个同类型的List<T>返回差异List<T>集合
        
/// Created by lhxhappy
        
/// 2009-01-05 23:12:30
        
/// </summary>
        
/// <typeparam name="T">泛型类型</typeparam>
        
/// <param name="newModel">修改后的数据集合</param>
        
/// <param name="oldModel">原始数据集合</param>
        
/// <param name="keyField">数据主键</param>
        
/// <returns>返回与原始集合有差异的集合</returns>
        
/// <summary>
        private List<T> GetModiflyList<T>(List<T> newModel, List<T> oldModel, string keyField)
        {
            
int conin = 0;
            List
<T> list = new List<T>();
            
foreach (T newMod in newModel)
            {
                conin
++;
                
//取得新实体的数据主键值
                object nob = newMod.GetType().GetProperty(keyField).GetValue(newMod, null);
                
if (nob == null)
                    nob 
= (object)string.Empty;


                
bool IsExist = false;
                
foreach (T oldMol in oldModel)
                {
                    conin
++;
                    
//取得老实体对象的属性集合
                    PropertyInfo[] pi = oldMol.GetType().GetProperties();
                    
//定义记数器
                    int i = 0;

                    
//取得老实体的数据主键值
                    object ob = oldMol.GetType().GetProperty(keyField).GetValue(oldMol, null);
                    
if (ob == null)
                        ob 
= (object)string.Empty;

                    
//如果新老实体主键值相等则进行属性的比较
                    if (object.Equals(nob, ob))
                    {

                        
//将老实体对象的没一个属性值和新实体对象进行循环比较
                        foreach (PropertyInfo p in pi)
                        {
                            conin
++;
                            
//防止object.Equals时实例化对象发生异常
                            object o_new = newMod.GetType().GetProperty(p.Name).GetValue(newMod, null);
                            
if (o_new == null)
                                o_new 
= (object)String.Empty;

                            
//防止object.Equals时实例化对象发生异常
                            object o_old = p.GetValue(oldMol, null);
                            
if (o_old == null)
                                o_old 
= (object)String.Empty;

                            
//如果有差异则保存当前实体
                            if (object.Equals(o_new, o_old))
                                
continue;
                            
else
                            {
                                list.Add(newMod);
                                
break;
                            }
                        }
                    }
                }
            }
            
return list;
        }

 

效率更高的写法,

 

 

 /// 对比两个同类型的List<T>返回差异List<T>集合
        
/// Created by lhxhappy
        
/// 2009-01-05 23:12:30
        
/// </summary>
        
/// <typeparam name="T">泛型类型</typeparam>
        
/// <param name="newModel">修改后的数据集合</param>
        
/// <param name="oldModel">原始数据集合</param>
        
/// <param name="keyField">数据主键</param>
        
/// <returns>返回与原始集合有差异的集合</returns>
        private List<T> GetModiflyList<T>(List<T> newModel, List<T> oldModel, string keyField)
        {
            
int conint = 0;
            List
<T> list = new List<T>();
            
foreach (T newMod in newModel)
            {
                conint
++;

                
//取得新实体的数据主键值
                object nob = newMod.GetType().GetProperty(keyField).GetValue(newMod, null);
             

                
//根据主建找到老实体集合中主键值相等的实体
                T oldMol = oldModel.Find((delegate(T old)
                       {
                           
object ob = old.GetType().GetProperty(keyField).GetValue(old, null);

                           
if (object.Equals(ob, nob))
                               
return true;
                           
else
                               
return false;
                       }));

                PropertyInfo[] pi 
= oldMol.GetType().GetProperties();

                
//将老实体对象的没一个属性值和新实体对象进行循环比较
                foreach (PropertyInfo p in pi)
                {
                    conint
++;

                    
object o_new = p.GetValue(newMod, null);
                    
object o_old = p.GetValue(oldMol, null);
                   
                    
//新老实体比较并记录有差异的实体
                    if (object.Equals(o_new, o_old))
                        
continue;
                    
else
                    {
                        list.Add(newMod);
                        
break;
                    }
                }

            }
            
return list;
        }

 


 


 

posted on 2009-01-05 01:12 lhx 阅读(...) 评论(...) 编辑 收藏