datatable - How to remove complete row from a data table when it is a duplicate row in c# -
i have data table has duplicate row follow.
| | | | | | |cid | usrnme | pname | prate | cabin | |-----------------------------------------------------------| |c11 | demo1@gmail.com | sample1 | 2000 | b2 | ******* |c14 | demo2@live.com | sample2 | 5000 | b3 | |c15 | demo3@yahoo.com | sample3 | 8000 | b2 | |c11 | demo1@gmail.com | sample1 | 2000 | b2 | ******* |c18 | demo4@gmail.com | sample4 | 3000 | l1 | |c11 | demo5@gmail.com | sample5 | 7400 | b4 | &&&&&&& ============================================================
note : there different data same id ,see &&&&&&& row
how 1 row above duplicate 2 rows.i have tried this
this code used.
public datatable removeduplicaterows(datatable dtable,string colname) { colname = "cabin"; hashtable htable = new hashtable(); arraylist duplicatearraylist = new arraylist(); foreach(datarow drow in dtable.rows) { if (htable.contains(drow[colname])) duplicatearraylist.add(drow); else { htable.add(drow[colname], string.empty); } } foreach (datarow drow in duplicatearraylist) dtable.rows.remove(drow); return dtable; }
if used above code avoid duplicate according cabin
removes all records cabin b2 , keep first 1 only.what want remove full row(keep 1 , delete others).how can that.
its cid
decides uniqueness of record. in provided example there 2 rows same cid
, entire row same too.. proves if use cid
find duplicates desired output
change line of code
colname = "cabin";
colname = "cid";
Comments
Post a Comment