C# 中DataTable合并以及去除重复数据

代码如下:
合并:

/// <summary>
/// 合并表结构相同datatable数据
/// </summary>
public DataTable Mergedata(){ 
 DataTable data = new DataTable();
 DataTable deptdata = new DataTable(); 
 
 data=DBHelper.FillDataTable("select * from T1", null);
 deptdata=DBHelper.FillDataTable("select * from T2", null);

 data.Merge(deptdata, true); 
 return  data;
}

剔除重复数据:

	/// <summary>
	/// 去除datatable重复数据
	/// </summary>
	public DataTable distinctdata() 
	{ 
	 DataTable data = new DataTable();
	 DataTable distinctdata = new DataTable();

	  List<string> datarowlist = new List<string>();
      string[] datarow;
      
     foreach (DataColumn row in data.Columns) //遍历出表头
      { 
       datarowlist.Add(row.ToString());
      }
     datarow = datarowlist.ToArray();
     
     DataView dv = new DataView(data);         
     distinctdata = dv.ToTable(true, datarow); //对表所有字段进行过滤,true表示使用distinct方法
	 return  distinctdata;
}
    原文作者:四川阿米
    原文地址: https://blog.csdn.net/weixin_42577016/article/details/106385910
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞