Use LINQ (group by having) to find duplicate data in the datatable

Source: Internet
Author: User
Private void butcf_click (Object sender, routedeventargs e) {If (datagriddatatable! = NULL & datagriddatatable. Rows. Count> 0 ){
// Group by date, contract number, department having count (Record Number)> 0 find the duplicate record var query = (from T in datagriddatatable. asenumerable () group T by new {T1 = T. field <datetime> ("date"), T2 = T. field <int64> ("Contract No."), T3 = T. field <string> ("Department")} into M select new {date = m. key. t1, Contract No. = m. key. t2, Department = m. key. t3, record number = m. first (). field <decimal> ("Record Number"), // write this sentence here, otherwise query rowcount = m cannot be found. count ()} into C Where C. rowcount> 1 select C ). tolist (); datatable dt = new datatable (); // DT. columns. add ("Serial Number", system. type. getType ("system. int32 "); DT. columns. add ("date", system. type. getType ("system. datetime "); DT. columns. add ("Contract No.", system. type. getType ("system. int64 "); DT. columns. add ("department", system. type. getType ("system. string "); DT. columns. add ("Record Number", system. type. getType ("system. decimal "); DT. columns. add ("sales amount", system. type. getType ("system. decimal "); foreach (var q in query) {datarow [] dr_finds = datagriddatatable. select ("date = '" + q. date + "'and Contract No. =" + q. contract No. + "and department = '" + q. department + "'"); foreach (datarow find in dr_finds) {datarow DR = DT. newrow (); Dr ["date"] = find ["date"]; Dr ["Contract No."] = find ["Contract No."]; dr ["department"] = find ["department"]; Dr ["Record Number"] = find ["Record Number"]; dr ["sales amount"] = find ["sales amount"]; DT. rows. add (DR) ;}} if (DT. rows. count> 0) {fmcfjl win = new fmcfjl (DT); win. show ();} else {MessageBox. show ("No Repeated Records"); Return ;}}}

 

Use LINQ (group by having) to find duplicate data in the datatable

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.