How to remove duplicate records from the DataTable based on 2 Column Names

Hello Guys,

Can anyone please provide me query to Remove Duplicate record from the Data Table based on 2 Column Names?

With below query I can remove duplicate rows based on 1 column name.
dt.AsEnumerable().GroupBy(Function(i) i.Field(Of String)("ColumnName")).Select(Function(g) g.First).CopyToDataTable()

but actually I want to remove duplicate records from the dataTable based on 2 column values so don’t know how to add another column in above query or if you can provide me the query to achieve this would be great.

In this example I want to remove duplicate records based on Column1 and Column3:

Input DataTable:
Column1 | Column2 | Column3
1234 | abc | Test1
1234 | xyz | Test1
1234 | pqr | Test2
5678 | abc | Test2
5678 | xyz | Test2
5678 | pqr | Test1

Output DataTable should be:
Column1 | Column2 | Column3
1234 | abc | Test1
1234 | pqr | Test2
5678 | abc | Test2
5678 | pqr | Test1

Thanks you,
Navneet
Happy Automation! :slight_smile:

@NavneetP
The case is about grouping and taking the first member from the group. It as specific form of finding the distincts

Here the linq can Help, but more easy to write within the query Syntax.

The key idea is to filter on the groups by taking the first group member

Filter activity can only remove rows specific to single criteria (e.g. Column1=1234 and Column3=“Test1”) and I don’t want to use any loop to do this.

Thanks,
Navneet :slight_smile:

Right, and i was Not talking about Filter Activity. With the Terms where, nonduplicated, groupocount a groupby approach with linq was mentioned, adressing your case

With below query I can remove duplicate rows based on 1 column name.
dt.AsEnumerable().GroupBy(Function(i) i.Field(Of String)("ColumnName")).Select(Function(g) g.First).CopyToDataTable()

but don’t know how to add another column in above query or if you can provide me the query to achieve this would be great.

Thanks,
Navneet :slight_smile:

@NavneetP
Find starter Help here:
FindDistinctsFromGroup_By2Cols.xaml (7.4 KB)

2 Likes

Thanks you very much for providing solution to the issue. That’s the exact solution I was looking for. :slight_smile:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.