Compare 2 column values and delete identical rows through LINQ

studio

#1

Hello,
I have 1 data table such as dt1dt1

I want to remove rows where ID and Name both have same values. I wanna achieve it applying LINQ code.

Output should be 1st and 4th row.

Please help.
Thank you


#2

Hi @Chand

First the links that support this answer: here and here.

The query would look like that:
From r In yourDataTable
Group By Distinct = New With {Key .ID = CInt(r(“ID”)), Key .Name = CStr(r(“Name”))} Into Group
Where Group.Count = 1
Select Distinct

The second link gives clues how to convert the LINQ result into a Data Table.


#3

In this case you can use standard datatable features.

Assign DistinctDt = dt.DefaultView.ToTable(“Test”, True, “ID”, “Name”)


#4

Sorry, @Chand I’ve misread your requirments in first comment. But your can solve it with one liner here if we extending above solution of @loginerror

Assign newDt =
(From r In yourDataTable
Group By Distinct = New With {Key .ID = CInt(r(“ID”)), Key .Name = CStr(r(“Name”))} Into Group
Where Group.Count = 1
From g In Group
Select g
).CopyToDatatable


#5

And the workflow as xaml file, just because I wanted to test it myself so I might as well post it :slight_smile:
DistintRows.zip (12.4 KB)


Remove rows or duplicates from datatable