C#编程,对于DataTable类型数据进行去重。

版权声明:我不生产代码,我只是代码的搬运工。 https://blog.csdn.net/qq_43307934/article/details/87782276

1、数据库直接去除重复

select distinct * from 表名或者另一个select语句。

2、对 DataTable直接进行操作

DataTable dt1=new DataTable(...);  
DataTable dt2 = dt1.DefaultView.ToTable(true, "列1,列2,列3");

这个时候 dt2就是去除了重复的行了
    
解释一下:

ToTable(true, "列1,列2,列3");
第一个参数,true 去除重复,false 不去除
第二个参数,需要显示的字段,这里显示"列1,列2,列3"三列。

其中, ToTable()有几种写法:

写法1:ToTable(true, "列1,列2,列3");

写法2:ToTable(true,  "列1", "列2", "列3");

写法3:string[] strComuns = { "列1", "列2", "列3"};
             ToTable(true, strComuns); 


写法4:ToTable(true, new string[] { "列1,列2,列3" });

猜你喜欢

转载自blog.csdn.net/qq_43307934/article/details/87782276