It also shows that, in addition to the DataView way to deal with, when a person think this is a little bad use. There's not much to explain here.
Code is simple, look directly at the code
usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Linq;namespacedts{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"] ="Xiaodong"; _dr["Address"] ="Beijing"; _dt. Rows.Add (_DR); _dt. Rows.Add (_DR. ItemArray); _dt. Rows.Add (_DR. ItemArray); _DR=_dt. NewRow (); _dr["ID"] =2; _dr["name"] ="Xiaodong"; _dr["Address"] ="Shanghai"; _dt. Rows.Add (_DR); _DR=_dt. NewRow (); _dr["ID"] =3; _dr["name"] ="xiaoming"; _dr["Address"] ="Guangdong"; _dt. Rows.Add (_DR); _DR=_dt. NewRow (); _dr["ID"] =4; _dr["name"] ="Xudaxia"; _dr["Address"] ="Fujian"; _dt. Rows.Add (_DR); _dt. Rows.Add (_DR. ItemArray); Console.WriteLine ("--------------------table with duplicate data originally----------------------"); _dt. AsEnumerable (). ToList (). ForEach (M={Console.WriteLine (m["ID"]. ToString () +" "+ m["name"]. ToString () +" "+ m["Address"]. ToString ()); }); Console.WriteLine ("--------------------Remove the duplicate 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.read (); } Public classDatatablerowcompare:iequalitycomparer<datarow> { #regionIequalitycomparer<datarow> Members Public BOOLEquals (DataRow x, DataRow y) {return((x.field<int> ("ID") = = y.field<int> ("ID") && x.field<string> ("name") = = y.field<string> ("name") && x.field<string> ("Address") = = y.field<string> ("Address"))); } Public intGetHashCode (DataRow obj) {returnobj. ToString (). GetHashCode (); } #endregion } }}
Use LINQ to resolve DataTable deduplication data duplication