Using System; using System. collections. generic; using System. data; using System. linq; using System. text; using System. threading. tasks; namespace RemoveDupRowDemoTest {class Program {static void Main (string [] args) {DataTable _ dt = new DataTable (); _ dt. columns. add ("id", typeof (int); _ dt. columns. add ("name", typeof (string); _ dt. columns. add ("address", typeof (string); DataRow _ dr = _ dt. newRow (); _ dr ["id"] = 1; _ dr ["name"] = "lipeng"; _ dr ["address"] = "DongXiaoKou "; _ dt. rows. add (_ dr); _ dt. rows. add (_ dr. itemArray); _ dt. rows. add (_ dr. itemArray); _ dr = _ dt. newRow (); _ dr ["id"] = 2; _ dr ["name"] = "xiaoNa"; _ dr ["address"] = "DongXiaoKou "; _ dt. rows. add (_ dr); _ dr = _ dt. newRow (); _ dr ["id"] = 3; _ dr ["name"] = "BingLi"; _ dr ["address"] = "Tianyi gyuan "; _ dt. rows. add (_ dr); _ dt. rows. add (_ dr. itemArray); Console. writeLine ("-------------------- the original Table ----------------------"); _ dt. asEnumerable (). toList (). forEach (x => {Console. writeLine (x ["id"]. toString () + "" + x ["name"]. toString () + "" + x ["address"]. toString () ;}); Console. writeLine (); Console. writeLine ("-------------------- use Linq to repeat the Table --------------------"); var _ comPresult = _ dt. asEnumerable (). distinct (new DataTableRowCompare (); DataTable _ resultDt = _ comPresult. copyToDataTable (); _ resultDt. asEnumerable (). toList (). forEach (x => {Console. writeLine (x ["id"]. toString () + "" + x ["name"]. toString () + "" + x ["address"]. toString () ;}); Console. writeLine (); Console. writeLine ("-------------------- use DefaultView to repeat the Table ----------------------"); DataTable _ dtDefalut = _ dt. defaultView. toTable (true, "id", "name", "address"); _ dtDefalut. asEnumerable (). toList (). forEach (x => {Console. writeLine (x ["id"]. toString () + "" + x ["name"]. toString () + "" + x ["address"]. toString () ;}); Console. readLine () ;}} public class DataTableRowCompare: IEqualityComparer <DataRow >{# region IEqualityComparer <DataRow> member public bool Equals (DataRow x, DataRow y) {return (x. field <int> ("id") = y. field <int> ("id");} public int GetHashCode (DataRow obj) {return obj. toString (). getHashCode () ;}# endregion }}
Go to: remove the duplicate data from the DataTable (program example comparison)