gpt4 book ai didi

c# - 不使用 LINQ 从数据表中删除重复的列值

转载 作者:行者123 更新时间:2023-12-02 16:20:44 25 4
gpt4 key购买 nike

考虑我的数据表,

Id  Name  MobNo
1 ac 9566643707
2 bc 9944556612
3 cc 9566643707

如何在不使用 LINQ 的情况下删除 C# 中包含重复 MobNo 列值的行 3。我在 SO 上看到过类似的问题,但所有答案都使用 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;
}

关于c# - 不使用 LINQ 从数据表中删除重复的列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2895066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com