usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceremoveduprowdemotest{classProgram {Static voidMain (string[] args) {DataTable _dt=NewDataTable (); _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"] ="Tiantongyuan"; _dt. Rows.Add (_DR); _dt. Rows.Add (_DR. ItemArray); Console.WriteLine ("--------------------table with duplicate data originally----------------------"); _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 (NewDatatablerowcompare ()); 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 classDatatablerowcompare:iequalitycomparer<datarow> { #regionIequalitycomparer<datarow> Members Public BOOLEquals (DataRow x, DataRow y) {return(x.field<int> ("ID") = = y.field<int> ("ID")); } Public intGetHashCode (DataRow obj) {returnobj. ToString (). GetHashCode (); } #endregion }}
To the DataTable to heavy