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

datatable
excel
activities

#1

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.


#2

@anhth15, Use LINQ Query,

Assign Activity:

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

Regards,
Dominic :slight_smile:


#3

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.


#4

@anhth15,

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

Regards ,
Dominic :slight_smile:


#5

Many thanks,
It works now.


#6

hi @599712,

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


#7

@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


#8

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


#9

@anhth15, Just replace the Column Name with Index.

Regards,
Dominic :slight_smile:


#10

Hi @599712,

It works perfectly.
Many thanks.


#11

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!