虽然DataTable.Merge可以很好的实现,但以下代码写出来更好理解

DataTable DataTable1 = new DataTable(); 
DataTable DataTable2 = new DataTable(); 
DataTable newDataTable = DataTable1.Clone(); 

object[] obj = new object[newDataTable.Columns.Count]; 
for (int i = 0; i < DataTable1.Rows.Count; i++) 
{ 
    DataTable1.Rows[i].ItemArray.CopyTo(obj,0); 
    newDataTable.Rows.Add(obj); 
} 
for (int i = 0; i < DataTable2.Rows.Count; i++) 
{ 
    DataTable2.Rows[i].ItemArray.CopyTo(obj,0); 
    newDataTable.Rows.Add(obj); 
}
//或者
DataTable DataTable1 = new DataTable(); 
DataTable DataTable2 = new DataTable(); 

object[] obj = new object[DataTable1 .Columns.Count]; 
for (int i = 0; i < DataTable2.Rows.Count; i++) 
{ 
    DataTable2.Rows[i].ItemArray.CopyTo(obj,0); 
    DataTable1.Rows.Add(obj); 
}
View Code

两个结构不同的DataTable合并

  /// <summary> 
  /// 将两个列不同的DataTable合并成一个新的DataTable 
  /// </summary> 
  /// <param name="dt1">Table表1</param> 
  /// <param name="dt2">Table表2</param> 
  /// <param name="DTName">合并后新的表名</param> 
  /// <returns></returns>

  private DataTable UniteDataTable( DataTable dt1 ,DataTable dt2 ,string DTName) 
  {  
   DataTable dt3 = dt1.Clone(); 
   for( int i = 0 ;i < dt2.Columns.Count ;i ++ ) 
   { 
    dt3.Columns.Add( dt2.Columns[i].ColumnName ) ; 
   } 
   object[] obj = new object[dt3.Columns.Count]; 
    
   for (int i = 0; i < dt1.Rows.Count; i++) 
   { 
    dt1.Rows[i].ItemArray.CopyTo(obj,0); 
    dt3.Rows.Add(obj); 
   } 
       
   if( dt1.Rows.Count >= dt2.Rows.Count ) 
   { 
    for( int i = 0 ;i < dt2.Rows.Count ;i++ ) 
    { 
     for( int j = 0 ;j < dt2.Columns.Count ;j ++ ) 
     { 
      dt3.Rows[i][j+dt1.Columns.Count] = dt2.Rows[i][j].ToString() ; 
     } 
    } 
   } 
   else 
   { 
    DataRow dr3 ; 
    for( int i = 0 ;i < dt2.Rows.Count - dt1.Rows.Count ;i ++ ) 
    { 
     dr3 = dt3.NewRow() ; 
     dt3.Rows.Add( dr3 ) ; 
    } 
    for( int i = 0 ;i < dt2.Rows.Count ;i++ ) 
    { 
     for( int j = 0 ;j < dt2.Columns.Count ;j ++ ) 
     { 
      dt3.Rows[i][j+dt1.Columns.Count] = dt2.Rows[i][j].ToString() ; 
     } 
    } 
   } 
   dt3.TableName = DTName ; 
   return dt3 ; 
  } 
View Code

 

今天一位朋友问如何去掉DataTable里重复的记录(DataTable是别人返回过来的,不能再重新查询数据库,所以无法用sql中的select distinct xxx处理,只能在DataTable上动脑筋) 

思路:将DataTable转成IEnumerable,然后就能调用Distinct方法了

System.Collections.Generic; 
using System.Linq; 
using System.Data; 
using System; 

namespace ConsoleApplication2 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            DataTable tbl = new DataTable(); 
            tbl.Columns.Add("Id", typeof(System.Int32)); 
            tbl.Columns.Add("City", typeof(System.String)); 
            tbl.Columns.Add("Province", typeof(System.String)); 

            tbl.Rows.Add(1, "武汉", "湖北"); 
            tbl.Rows.Add(2, "应城", "湖北"); 
            tbl.Rows.Add(3, "武汉", "湖北"); 

            IEnumerable <DataRow> r = tbl.AsEnumerable().Distinct(new CityComparer()); 
          
          

            //到这一步,r里就是去重复的记录了 

            foreach (var item in r) 
            { 
                Console.WriteLine(item["Id"] + "" + item["City"] + "" + item["Province"]); 
            } 

            Console.ReadLine(); 
        } 


        
    } 

    class CityComparer : IEqualityComparer <DataRow> 
    { 
        public bool Equals(DataRow r1, DataRow r2) 
        { 
            return r1["City"] == r2["City"]; 
        } 

        public int GetHashCode(DataRow obj) 
        { 
            return obj.ToString().GetHashCode(); 
        } 


    } 
}
View Code

上面的代码,将DataTable中"城市名"重复的记录去掉了,以上代码同样适用于List<T>(只要改下"比较器"即可)

posted on 2013-12-31 21:39  cpu歌歌  阅读(588)  评论(0编辑  收藏  举报