C# - 从数据表删除与相同的列值的行 [英] C# - Remove rows with the same column value from a DataTable

查看:99
本文介绍了C# - 从数据表删除与相同的列值的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据表看起来像这样:

I have a DataTable which looks like this:

 ID   Name    DateBirth
.......................
 1     aa      1.1.11
 2     bb      2.3.11
 2     cc      1.2.12
 3     cd      2.3.12

这是最快的方法用相同的ID删除行,要得到这样的事情(保持第一次出现,删除接下来的):

Which is the fastest way to remove the rows with the same ID, to get something like this (keep the first occurrence, delete the next ones):

 ID   Name    DateBirth
.......................
 1     aa      1.1.11
 2     bb      2.3.11
 3     cd      2.3.12

我不想两次通过表中的行,因为行数量大。
我想如果可能的话用一些LINQ中,但我想这将是一个很大的查询,我必须用一个比较器。

I don't want to double pass the table rows, because the row number is big. I want to use some LinQ if possible, but I guess it will be a big query and I have to use a comparer.

推荐答案

您可以使用LINQ到数据表,基于列不同的 ID ,你可以的按组的此列,然后执行选择的第一:

You can use LINQ to DataTable, to distinct based on column ID, you can group by on this column, then do select first:

  var result = dt.AsEnumerable()
                 .GroupBy(r => r.Field<int>("ID"))
                 .Select(g => g.First())
                 .CopyToDataTable();

这篇关于C# - 从数据表删除与相同的列值的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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