方法1,使用Linq查询表达式。
代码:
复制代码 代码示例:
DataTable testtable = new DataTable();
testtable.Columns.Add("ID");
testtable.Columns.Add("ProductName");
testtable.Rows.Add("1", "1");
testtable.Rows.Add("1", "1");
testtable.Rows.Add("1", "1");
testtable.Rows.Add("2", "2");
testtable.Rows.Add("3", "3");
DataTable finalltable = new DataTable();
finalltable = testtable.Clone();
finalltable.Clear();
var rows = from row in testtable.AsEnumerable() group row by row["ID"] into myrow select myrow.FirstOrDefault();
foreach (DataRow row in rows)
{
finalltable.ImportRow(row);
}
方法2,使用dataview来过滤datatable,去除重复行:
testtable = testtable.DefaultView.ToTable(true, new string[] { "ID", "ProductName" });