Go to: remove the duplicate data from the DataTable (program example comparison)

Source: Internet
Author: User
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)

Related Article

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.