How to put data from a column of datatable to list of string

Hi everyone,

I am finding solution to put data from a column in a datatable into list of string. At the moment, I loop row by row in datatable and add each item value into list of string. I want to find a better solution for that.
Please help me if you have any idea.

Many thanks,
AnhTH15.

3 Likes

@anhth15, Use LINQ Query,

Assign Activity:

List(of string) = From row in dt.AsEnumerable() Select Convert.Tostring(row(“ColumnName”)).ToList()

Regards,
Dominic :slight_smile:

It throws an error with content as below:
Cannot assign from type ‘System.Data.EnumerableRowCollection’1[System.Collections.Generic.List’1[System.Char]]’ to type ‘System.Collections.Generic.List’1[System.String]’ in Assign activity’Assign.’
Please help.

@anhth15,

Edited Missed braces ()
( From row in dt.AsEnumerable() Select Convert.Tostring(row(“ColumnName”)) ).ToList()

Regards ,
Dominic :slight_smile:

10 Likes

Many thanks,
It works now.

hi @599712,

Instead of using “ColumnName”, how could I use ColumnIndex in that query?

@anhth15 Did you mean something like

using assign activity
list_var = new List(of string)

use for each of data table -

using add to collection item activity
collection = list_var
Item = row(column-index).ToString

1 Like

I mean how could I use LinQ with column index value instead of column name value?

@anhth15, Just replace the Column Name with Index.

Regards,
Dominic :slight_smile:

Hi @599712,

It works perfectly.
Many thanks.

Hi Dominic,

How could I specify a [Where] clause whitin the select query…? I Want to make a list from a datatable but I need to specify conditions for one specific column. Thank you!