How to find duplicate line items in the first column and adding a duplicate count info on it

Hi Team,

I have a datatable dt , I need to find the duplicate Column items of column A

please help

1 Like

Hello @Karan28,

You can try this syntax.

dtVar =dtVar.AsEnumerable().GroupBy(Function(i) i.Field(Of String)(“Column Name”)).Select(Function(g) g.First).CopyToDataTable

Cheers
@Karan28

@Karan28
find a starter help here, showcasing different scenarios:
FindDupsUniquesFirstFromGroup_By1Col.xaml (8.0 KB)

1 Like

Thankyou for this guys , but let me explain you my exact query.

My ultimate aim is not to have duplicate items in the column A of DT.

So first I have to check if I have duplicate items or not , if yes I have to loop through duplicate items and make it unique. Like I can loop through those items and add counter at the last and increase the counter by 1 in each loop.

For ex:-

ColumnA ColumnB ColumnC

Rahul 90 121
Shreya 89 111
Rahul 99 000
Karan 00 444

So here I have duplicate items in row 1 and row 2.

So I can loop through the duplicate items and add counter after that

like this :- row(0) = row(0) + Counter

Counter = counter +1

and final output dt is :-
ColumnA ColumnB ColumnC
Rahul1 90 121
Shreya 89 111
Rahul2 99 000
Karan 00 444

@Karan28
Find starter help here

Without LINQ:
AddCounterToDups_IfDupRow-NoLINQ.xaml (10.1 KB)

With LINQ:
AddCounterToDups_IfDupRow-LINQ.xaml (9.2 KB)

Input:
grafik

Output:
grafik

Let us know your feedback

Hello ,

I have tested it and its working fine but when I merged it in my code its not changing the duplicates in the first column.

I just changed the dtData datatable with my datatable and as far as I understand the logic I dont need to change the column name or number

@Karan28
it is recommended to set the columnnames to a reliable reference to ensure that the right column is picked up. So the columnname is helpfully for this especially the code is merged from different sources.

If data comes from EXCEL just check if maybe some blanks after the values in the checked column is confusing the code. Also just debug the code to inspect more the flow.

Please ensure that you are sharing always all relevant information (requirements, issue description) is with us. So we can faster react and adress the things more directly. We assume that you are working with the NON-Linq approach, right?

Yes , first I had implemented the Linq approach then I have applied the Non- Linq approach.

Now non- Linq approach is working fine , also can this logic deals with null values foe ex if first column have two null values which means two same values then this logic will give 1 & 2 instead of null values?

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