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