I have a datatable with 3 columns How to find distinct values in one column of data table and output to be 3 column datatable as follows

I have a data table
example:
Code Value Amount
A001 1 10000
A001 1 20000
A002 1 10000
A003 1 10000
A002 1 20000
A003 1 20000
A004 1 20000
A005 1 20000

I need to get distinct values from code column and need to display the rest of two columns also in output as datatable
Code Value Amount
A001 1 10000
A002 1 10000
A003 1 10000
A004 1 20000
A005 1 20000

Hi @nikhil.s,

Distinct Based on the specific column name from the data table.

YourDataTable=YourDataTable.DefaultView.ToTable(true, “Code”)

Code → your column name

Hope it will work☺️

T,
Guna

1 Like

I used that method but it is giving output only one column i need values from 2nd and 3rd column also

Hey @nikhil.s

Please try the Below LINQ query.

YourDataTable = YourDataTable .AsEnumerable().GroupBy(Function(f) f.Field(Of String)(“Code”)).Select(Function(e) e.First).CopyToDataTable

T,
Guna

5 Likes

@nikhil.s
find some starter help here introducing on retreiving, duplicates, nonduplicates or distrincts based on 1 key column:
FindDupsUniquesFirstFromGroup_By1Col.xaml (8.0 KB)

1 Like

Hi @ppr,
I tried to understand your xaml and I have got one question: Which part of your assign statement defines the column to search for duplicate or distinct values?
Is it ‘k=d(0)’ in
(From d In dtData
Group d By k=d(0).toString.Trim Into grp=Group
Select grp.First()).CopyToDatatable

and (0) is the column index?

Thanks for your help!
Moritz

yes, this is the part of the grouping criteria

yes. Also Column Name can be used, if it is preferred.

1 Like

Wow, very fast answer!

That helps me, thank you!!

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