从数据表csharp中删除重复的行 [英] remove duplicate row from datatable csharp
问题描述
hi
我想剪切datatable上的所有重复行并复制到这样的其他数据表:
datatablefirst:
ID 姓名
123 Joe
123 John
124 Thro
125奥巴马
126 Jenifer
127杰克
127 Arnent
127 Joseph
128麦当娜
129马里
Datatablenonduplicate:
ID 姓名
124 Thro
125奥巴马
126 Jenifer
128麦当娜
129马里
Datatableduplicate:
ID 姓名
123 Joe
123 John
127 Jack
127 Arnent
127 Joseph
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
推荐答案
你可以检查以下链接以获取不同选项:
http://stackoverflow.com/questions/340223/what-is-the-best-way-to-remove-duplicates-from-a-datatable 一> [ ^ ]
you can check the following link for different options:
http://stackoverflow.com/questions/340223/what-is-the-best-way-to-remove-duplicates-from-a-datatable[^]
你可以这样做。
使用linq查询来检索重复项,如下所示。
循环遍历重复行并将行添加到重复的表中。
最后从现有表中删除重复项以获取唯一行。
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);
}
}
表将包含唯一行,datatableduplicate包含重复行。
table will contain unique rows and datatableduplicate contain duplicates rows.
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
这篇关于从数据表csharp中删除重复的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!