DataTable get all unique values from a column

Hi, what’s the most efficient method to get a list or array of all the unique values in a column from a DataTable? Preferably without looping, thanks

2 Likes

Filter based on one column -

yourDT.DefaultView.ToTable(true, "ColumnName")

Filter based on more than one column -

yourDT.DefaultView.ToTable(true, "ColumnName1", "ColumnName2"...."ColumnNameN")

Regards,
Karthik Byggari

21 Likes

did u get the solution for this?

Yes, it worked @vicky1234567890. Please find the workflow, use this as a function (Plug & Play).

GetDistinctDataTable.xaml (5.5 KB)

3 Likes

it’s working

1 Like

Hi Karthik, one correction I believe, it should be true instead of false.

1 Like

Thanks. You are right.

For distinct values - true.

Thank you for letting me know.

Hi one more question , If I need the datarows as well corresponding to the unique column values, how could that be done?

dt.AsEnumerable().GroupBy(Function(a) a.Field(Of String)(“name”)).Select(Function(b) b.First).CopyToDataTable()

You can use this one as well.

4 Likes

I don’t know why it is not working in my case…can you help, please
I want unique Numbers in sheet2
If I’m doing wrong please correct me.
Thanks

check.zip (23.0 KB)

If i dont have column headers then how to provide column number

Hi @Doonline ,

Try this -

(From row In dt_TransactionData
Group row By a=row(0).toString.Trim Into grp=Group
Select grp.First
).CopyToDatatable

here instead of 0 pass your column index.

Thanks

Hi @Doonline

Try this below expression

(From p in DT.Select() where( From q in DT.Select() where q(“NLC”).Equals(p(“NLC”)) Select q).ToArray.Count=1 Select p).ToArray.CopyToDataTable()

Hope it will help you.

Regards
Gokul

In this i am get hole column but I Want only the specified name in that column can you help me

Hey @KANTHAIAH_SAYALA are you referring to a specific Row in the Column? If so, I would suggest using the Filter Datatable Activity.

Hi All,

My scenario is similar.

I need to filter one column, but I still need all columns data. How can I achieve this?

For eg:

I have “Column 1”, “Column 2”, “Columns 3”, “Column 4”

I have to get unique values from “Column 1”, but I still need to have all four columns in the data table.