Get Unique Values in Column in a datatable along with required column

Hi All,

Need to a extract unique values in a column in datatable

I have datatable with company name, To address, cc address, Loan Numbers and so on

I need get unique values under company name column with corresponding values of to address and cc address

I used this query
dt.AsEnumerable.groupby(function(s) s(“CompanyName”).tostring).select(function(x) x.first()).copyToDatatable()

But this is returning unique values from companyName columns with all the rest columns but i need only three columns company name, To address and cc address

Thankyou in advance @ppr @Yoichi

@Kavya_S

Can you try this

dt.AsEnumerable.groupby(function(s) s(“CompanyName”).tostring).select(function(x) x.first()).copyToDatatable().defaultview.totable(false,"company name","To address","cc address")
1 Like

Hey @Kavya_S

If your goal is to get unique rows based on certain properties, DistinctBy is the right choice. If you want to group your rows by some criteria and possibly perform further operations on each group, then GroupBy is more appropriate.

Solutions:

If you need Unique values applies to all the 3 columns then,
Use Below mentioned LinQ:

dt_Output = dt_Output.DefaultView.ToTable(True,{"company name","To address","cc address"})

Screenshot for your reference:

And If Unique value should only apply to company name column then use below mentioned LinQ:

dt_Output = dt_Output.AsEnumerable().DistinctBy(Function(x) x("CompanyName").ToString.Trim).CopyToDataTable.DefaultView.ToTable(False,{"company name","To address","cc address"})

Screenshot for your reference:

Hope it helps!

Regards,
Ajay Mishra

2 Likes

@Shiva_Nikhil @Ajay_Mishra Thankyou for the solutions its working

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