Reputation: 713
I have 2 datatables, Tab1 and Tab2. Tab1 is full of data, Tab2 is empty.
Tab1 looks like that:
Col1 | Col2 | Col3 | Key | Country | Col....
abc | xyz | 103 | AK1 | POL| .....
pol | man | 1212 | AK2 | POL| ....
moro | kom | 11 | AK1 | POL | ....
bca | oni | 10a| AK1 | GER| ....
bca | oni | 10a| AK3 | GER| ....
al | 1n | zxc | AK2 | POL| ....
I need to select distinct data by Key and Country(Key+Country combination must be unique) and put it to Tab2.
How to do it in c#? I have a lot of rows, so I need quiet fast method to do it.
For this example a result should be:
Col1 | Col2 | Col3 | Key | Country | Col....
abc | xyz | 103 | AK1 | POL| .....
pol | man | 1212 | AK2 | POL| ....
bca | oni | 10a| AK1 | GER| ....
bca | oni | 10a| AK3 | GER| ....
Upvotes: 1
Views: 1896
Reputation: 203819
You can group the items on the given columns and then grab the first (or last, or whichever) row from each group.
secondTable = firstTable.AsEnumerable()
.GroupBy(row => new
{
Key = row.Field<string>("Key"),
Country = row.Field<string>("Country"),
})
.Select(group => group.First())
.CopyToDataTable();
Upvotes: 2