C# 去除DataTable表中重复行

去除DataTable表中重复行有两种方法:
一、利用sql语句的distinct 关键字
如:select distinct * from table_name;
二、利用DataView.ToTable()方法
1.DataView.ToTable()

根据现有DataView中的行,创建并返回一个新的DataTable。

2.DataView.ToTable(String)

根据现有DataView中的行,创建并返回一个新的DataTable。参数String为返回的DataTable的名称,输出的表与输入表的列相通,不可自定义。

3.DataView.ToTable(Boolean,String[])

根据现有DataView中的行,创建并返回一个新的DataTable。参数Boolean如果为true,则去重,为false时不去重,且默认为false。

可自定义返回的列,数组String[]为显示返回列的集合。

例子:

//去掉重复行
DataTable dt = db.GetDataTable("select * from 表名"); //获得datatable
DataView dv = dt.DefaultView;
table = dv.ToTable(true, new string[] { "name", "code" });

public DataTable GetDataTable(string strSql)
{
	try
	{
		//DataSet dataSet = new DataSet();
		//SqlDataAdapter adapter = new SqlDataAdapter(strSql, _Connection);
		//adapter.Fill(dataSet);
		//return dataSet.Tables[0]; 
		DataTable dt= new DataTable ();
        SqlDataAdapter adapter = new SqlDataAdapter(strSql, _Connection);
        adapter.Fill(dt);
        return dt;
	}
	catch
	{
		return null;
	}
}

如果有一组数据(id不是唯一字段)

namecode
张三123
李四123
张三456
张三123

通过上面的方法得到

namecode
张三123
李四123
张三456

4.DataView.ToTable(String,Boolean,String[])

根据现有DataView中的行,创建并返回一个新的DataTable。第一个参数string用来定义返回表的名称。

注意:想要的结果是只针对其中的一列去重,还要显示其他的列,怎么做

#region 删除DataTable重复列,类似distinct
        /// <summary>   
        /// 删除DataTable重复列,类似distinct   
        /// </summary>   
        /// <param name="dt">DataTable</param>   
        /// <param name="Field">字段名</param>   
        /// <returns></returns>   
        public static DataTable DeleteSameRow(DataTable dt, string Field)
        {
            ArrayList indexList = new ArrayList();
            // 找出待删除的行索引   
            for (int i = 0; i < dt.Rows.Count - 1; i++)
            {
                if (!IsContain(indexList, i))
                {
                    for (int j = i + 1; j < dt.Rows.Count; j++)
                    {
                        if (dt.Rows[i][Field].ToString() == dt.Rows[j][Field].ToString())
                        {
                            indexList.Add(j);
                        }
                    }
                }
            }
            indexList.Sort();
 // 排序
            for (int i = indexList.Count - 1; i >= 0; i--)// 根据待删除索引列表删除行  
            {
                int index = Convert.ToInt32(indexList[i]);
                dt.Rows.RemoveAt(index);
            }
            return dt;
        }

        /// <summary>   
        /// 判断数组中是否存在   
        /// </summary>   
        /// <param name="indexList">数组</param>   
        /// <param name="index">索引</param>   
        /// <returns></returns>   
        public static bool IsContain(ArrayList indexList, int index)
        {
            for (int i = 0; i < indexList.Count; i++)
            {
                int tempIndex = Convert.ToInt32(indexList[i]);
                if (tempIndex == index)
                {
                    return true;
                }
            }
            return false;
        }
        #endregion

借鉴文章:
1、C# DataTable去重,根据列名去重保留其他列
原文链接:https://blog.csdn.net/qq_23502409/article/details/75269221
2、C# 中怎样去除DataTable表里面的重复行
原文链接:https://blog.csdn.net/u010892197/article/details/50310907
3、c# DataView.ToTable() 方法 去除表中的重复项
原文链接:https://blog.csdn.net/JYL15732624861/article/details/61422332

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值