再看看我的数据表,删除从数据表中重复的列值,而无需使用LINQ
Id Name MobNo
1 ac 9566643707
2 bc 9944556612
3 cc 9566643707
如何去除其中包含重复在C#MobNo
列值,而无需使用LINQ行3
。我看到过类似的问题,但所有的答案都使用LINQ。
再看看我的数据表,删除从数据表中重复的列值,而无需使用LINQ
Id Name MobNo
1 ac 9566643707
2 bc 9944556612
3 cc 9566643707
如何去除其中包含重复在C#MobNo
列值,而无需使用LINQ行3
。我看到过类似的问题,但所有的答案都使用LINQ。
下面的方法做我想要什么....
public DataTable RemoveDuplicateRows(DataTable dTable, string colName)
{
Hashtable hTable = new Hashtable();
ArrayList duplicateList = new ArrayList();
//Add list of all the unique item value to hashtable, which stores combination of key, value pair.
//And add duplicate item value in arraylist.
foreach (DataRow drow in dTable.Rows)
{
if (hTable.Contains(drow[colName]))
duplicateList.Add(drow);
else
hTable.Add(drow[colName], string.Empty);
}
//Removing a list of duplicate items from datatable.
foreach (DataRow dRow in duplicateList)
dTable.Rows.Remove(dRow);
//Datatable which contains unique records will be return as output.
return dTable;
}
你可能想在DISTINCT上查看内部工作,然后在你的sharp数据库上运行这个工具(一定要备份!),但是如果它按我想的那样工作(抓住第一个值),你应该能够使用(非常类似的东西)的SQL语句:
DELETE FROM YourTable WHERE Id NOT IN (SELECT DISTINCT Id, MobNo FROM YourTable);
我从csv文件生成我的数据表... – 2010-05-24 06:20:08
@Pandiya,好吧,你为什么不这么说? =) – 2010-05-24 06:44:22
我已经发布了一个答案,这对我来说工作得很好...... – 2010-05-24 07:06:50
当你正在阅读的CSV文件(位的伪代码,但你得到的图片):
List<String> uniqueMobiles = new List<String>();
String[] fileLines = readYourFile();
for (String line in fileLines) {
DataRow row = parseLine(line);
if (uniqueMobiles.Contains(row["MobNum"])
{
continue;
}
uniqueMobiles.Add(row["MobNum"]);
yourDataTable.Rows.Add(row);
}
这不仅会将具有唯一移动设备的记录加载到数据表中。
可以在C#
这是最简单的方式使用 “的IEqualityComparer”。
**
var uniqueContacts = dt.AsEnumerable()
.GroupBy(x=>x.Field<string>("Email"))
.Select(g=>g.First());
** 我发现它在这个线程 LINQ to remove duplicate rows from a datatable based on the value of a specific row
到底是什么,对我来说,我回到它的数据表
DataTable uniqueContacts = dt.AsEnumerable()
.GroupBy(x=>x.Field<string>("Email"))
.Select(g=>g.First()).CopyToDataTable();
当发现重复,你想如何决定哪一个要保留,哪一个要删除? – 2010-05-24 06:07:26
@Tomas总是第一个留下来... – 2010-05-24 06:09:50
你想只获得一组带有唯一移动号码的记录,或者是从现有的组中删除重复的记录吗? – Strelok 2010-05-24 06:16:31