删除重复的记录 [英] Remove Duplicate records

查看:98
本文介绍了删除重复的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我想从包含字符串记录的数据表中删除重复的记录,有人可以帮我吗,我尝试了以下代码,但没有用.

Hello All,
I want to remove duplicate records from datatable contianing string records, can anybody will help me, i tried the below code but it didnt work.

public DataTable RemoveDuplicateRows(DataTable dTable, string colName)
        {
            Hashtable hTable = new Hashtable();
            ArrayList duplicateList = new ArrayList();
            foreach (DataRow dtRow in dTable.Rows)
            {
                if (hTable.Contains(dtRow[colName].ToString().ToUpper()))
                    duplicateList.Add(dtRow);
                else
                    hTable.Add(dtRow[colName].ToString().ToUpper(), string.Empty);
            }
            foreach (DataRow dtRow in duplicateList)
                dTable.Rows.Remove(dtRow);
            return dTable;
        }




谢谢
Anand




Thanks
Anand

推荐答案

根据您对我的评论的回答,您有一个具有单列的表,并且希望删除该表的重复数据,这是正确的理解吗? br/>
如果是,请检查以下来自以下来源的代码.应该可以做到的

Based on your answer to my comment, You have a table with single column and you want to de-duplicate that table, is that the correct understanding?

If yes, check the following code taken from source below. That should do the trick

DELETE MyTable
FROM MyTable
LEFT OUTER JOIN (
   SELECT MIN(RowId) as RowId, colName
   FROM MyTable
   GROUP BY colName
) as KeepRows ON
   MyTable.RowId = KeepRows.RowId
WHERE
   KeepRows.RowId IS NULL



来自以下代码: http://stackoverflow.com/questions/18932/how-can-i -remove-duplicate-rows [ ^ ]

希望有帮助.
Milind



Code from : http://stackoverflow.com/questions/18932/how-can-i-remove-duplicate-rows[^]

Hope that helps.
Milind


这篇关于删除重复的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆