Use LINQ to resolve DataTable deduplication data duplication

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.