Remove all duplicates rows based on a column

Hello,

I wanted to remove all duplicates including the original row based on column “Material” from the data table

Example : Input

Output

The output should not contain the duplicates.
Thank You,

Hi @Yugal_Raju ,
Refer this link, hope this helps

@Yugal_Raju

Try this linq

dtInput.AsEnumerable().GroupBy(Function(row)row(“Material”).ToString).Where(Function(g) g.Count() = 1).SelectMany(Function(g) g).CopyToDataTable()

1 Like

@Yugal_Raju,

Use this code in invoke code activity.

' Step 1: Identify the duplicate materials
Dim duplicateMaterials = (From row In dtInput.AsEnumerable()
                          Group row By material = row("Material").ToString.Trim Into grp = Group
                          Where grp.Count > 1
                          Select material).ToList()

' Step 2: Filter the DataTable and remove all rows with duplicate materials
Dim dtResult As DataTable = dtInput.AsEnumerable() _
    .Where(Function(row) Not duplicateMaterials.Contains(row("Material").ToString.Trim)) _
    .CopyToDataTable()

' Optional: Handle case when dtResult might be empty
If Not dtResult.Any() Then
    dtResult = dtInput.Clone() ' Returns an empty DataTable with the same structure
End If

LLM helped me to write this

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