Click here to Skip to main content
11,711,009 members (83,339 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: DataTable
hi

I want to cut all duplicated rows on datatable and copy to an other datatable like this:

datatablefirst:
ID Name
123 Joe
123 John
124 Thro
125 Obama
126 Jenifer
127 Jack
127 Arnent
127 Joseph
128 Madonna
129 Mali



Datatablenonduplicate:
ID Name
124 Thro
125 Obama
126 Jenifer
128 Madonna
129 Mali


Datatableduplicate:
ID Name
123 Joe
123 John
127 Jack
127 Arnent
127 Joseph
Posted 20-Sep-11 3:28am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Heres easy and best way to remove duplicate without using for loop.

private DataTable RemoveDuplicatesRecords(DataTable dt)
{
    //Returns just 5 unique rows
    var UniqueRows = dt.AsEnumerable().Distinct(DataRowComparer.Default);
    DataTable dt2 = UniqueRows.CopyToDataTable();
    return dt2;
}

[Click for more Detail]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You can do something like this.
use linq query to retrieve duplicates as shown below.
And loop through duplicate rows and add rows into duplicate table.
Finally remove duplicates from existing table to get unique rows.

DataTable table=new DataTable();
//DataTable Datatablenonduplicate = table.Clone();
DataTable Datatableduplicate = table.Clone();
var dupsFromCol = from dr in table.AsEnumerable()
                              group dr by dr["ID"] into groups
                              where groups.Count() > 1
                              select groups;
foreach(var duplicate in dupsFromCol)
{
 for(int i=0;i<duplicate.count();i++)>
{
DataRow dr = Datatableduplicate.NewRow();
dr=duplicate.ElementAt(i);
Datatableduplicate.ImportRow(dr);
table.Rows.Remove(dr); 
}
}

table will contain unique rows and datatableduplicate contain duplicates rows.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

with cte
as
(select id,count(id)'Countid' from EMP_TEST1 group by id
)
 
select E.ID,E.DEPT,E.GrossSalary,E.NAME,E.NetSalary,E.SCM into New_Table from cte c inner join EMP_TEST1 E on E.ID=c.ID where c.Countid>1;
 
with cte1
as
(select id,count(id)'Countid' from EMP_TEST1 group by id
)
select E.ID,E.DEPT,E.GrossSalary,E.NAME,E.NetSalary,E.SCM into New_Table1 from cte1 c inner join EMP_TEST1 E on E.ID=c.ID where c.Countid=1;
 

select * from New_Table
select * from New_Table1
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Quote:
with cte
as
(select id,count(id)'Countid' from EMP_TEST1 group by id
)

select E.ID,E.DEPT,E.GrossSalary,E.NAME,E.NetSalary,E.SCM into New_Table from cte c inner join EMP_TEST1 E on E.ID=c.ID where c.Countid>1;

with cte1
as
(select id,count(id)'Countid' from EMP_TEST1 group by id
)
select E.ID,E.DEPT,E.GrossSalary,E.NAME,E.NetSalary,E.SCM into New_Table1 from cte1 c inner join EMP_TEST1 E on E.ID=c.ID where c.Countid=1;

select * from New_Table
select * from New_Table1

  Permalink  
Comments
Afzaal Ahmad Zeeshan at 3 days ago
   
Can you explain what that is?
Richard Deeming at 3 days ago
   
It appears to be a repost of Solution #4, which was posted in March 2013 in response to a question from September 2011.

Or, in other words, a clear attempt at rep-hunting.
Afzaal Ahmad Zeeshan at 3 days ago
   
Or the problem that every new user has, gain reputation by answering old question and think they can walk away with it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 549
1 OriginalGriff 255
2 Peter Leow 154
3 Maciej Los 120
4 Abhinav S 120
0 Maciej Los 95
1 Richard MacCutchan 90
2 Abhinav S 90
3 OriginalGriff 80
4 Member 4653839 50


Advertise | Privacy | Mobile
Web02 | 2.8.150819.1 | Last Updated 28 Aug 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100