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

发布于 2024-09-03 01:43:45 字数 226 浏览 7 评论 0原文

考虑我的数据表,

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

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

Consider my datatable,

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

How to remove the row 3 which contains duplicate MobNo column value in c# without using LINQ. I have seen similar questions on SO but all the answers uses LINQ.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北城挽邺 2024-09-10 01:43:46

这是最简单的方法。

**

var uniqueContacts = dt.AsEnumerable()
                       .GroupBy(x=>x.Field<string>("Email"))
                       .Select(g=>g.First());

**
我在这个线程中找到了它
LINQ to根据特定行的值从数据表中删除重复行

对于我来说,我将其作为数据表返回实际上是什么

DataTable uniqueContacts = dt.AsEnumerable()
                           .GroupBy(x=>x.Field<string>("Email"))
                           .Select(g=>g.First()).CopyToDataTable();

This is the simplest way .

**

var uniqueContacts = dt.AsEnumerable()
                       .GroupBy(x=>x.Field<string>("Email"))
                       .Select(g=>g.First());

**
I found it in this thread
LINQ to remove duplicate rows from a datatable based on the value of a specific row

what actually was for me that I return it as datatable

DataTable uniqueContacts = dt.AsEnumerable()
                           .GroupBy(x=>x.Field<string>("Email"))
                           .Select(g=>g.First()).CopyToDataTable();
池予 2024-09-10 01:43:46

在你的尖锐数据库上运行它之前,你可能想查找 DISTINCT 的内部工作原理(一定要备份!),但如果它像我认为的那样工作(获取第一个值),你应该能够使用(某些东西)非常类似于)以下 SQL:

DELETE FROM YourTable WHERE Id NOT IN (SELECT DISTINCT Id, MobNo FROM YourTable);

You might want to look up the inner workings on DISTINCT before running this on your sharp DB (be sure to back up!), but if it works as I think it does (grabbing the first value) you should be able to use (something very similar to) the following SQL:

DELETE FROM YourTable WHERE Id NOT IN (SELECT DISTINCT Id, MobNo FROM YourTable);
沉鱼一梦 2024-09-10 01:43:46

您可以在 C# 中使用“IEqualityComparer”

You can use "IEqualityComparer" in C#

蓦然回首 2024-09-10 01:43:45

下面的方法做了我想要的......

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;
    }

The following method did what i want....

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;
    }
风吹过旳痕迹 2024-09-10 01:43:45

当您阅读 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);       
}

这只会将具有唯一手机的记录加载到您的数据表中。

As you are reading your CSV file ( a bit of pseudo code, but you get the picture ):

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);       
}

This will only load the records with unique mobiles into your data table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文