C# – vb.net LINQ select Distinct to a List

cdatatablelinqnetvb.net

I have a datatable with a column that has some duplicate values, I want to add those values to a listbox but without duplicates

I tried the following

Dim a = From row In table.AsEnumerable.Distinct.ToList Select row.Field(Of String)("name")

but it gives me duplicate values, How can it be done without duplicates?

Best Answer

I believe there are some more column(s) which are unique in each row that's why the distinct doesn't return the result as expected. Instead you should need to select the columns first than apply the distinct to it.

So try this instead :

Dim a = (From row In table.AsEnumerable()
        Select row.Field(Of String)("name")).Distinct().ToList()

Hope this will help !!