Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a column of DataTable to a List

I have a DataTable with multiple columns. I want to get a List<String> out of first column of DataTable. How can I do that?

like image 210
shrishjain Avatar asked Jul 07 '11 22:07

shrishjain


People also ask

How to convert DataTable into List in UiPath?

Select Convert.Tostring(row(“Name”))).ToList() Now, we are converting each row value that is related to that particular column value to a string and add it to List. This is how the LINQ expression will work in this case of converting a DataTable column to a list.


1 Answers

Try this:

static void Main(string[] args) {     var dt = new DataTable     {         Columns = { { "Lastname",typeof(string) }, { "Firstname",typeof(string) } }     };     dt.Rows.Add("Lennon", "John");     dt.Rows.Add("McCartney", "Paul");     dt.Rows.Add("Harrison", "George");     dt.Rows.Add("Starr", "Ringo");      List<string> s = dt.AsEnumerable().Select(x => x[0].ToString()).ToList();      foreach(string e in s)         Console.WriteLine(e);      Console.ReadLine(); } 
like image 103
Michael Buen Avatar answered Sep 17 '22 15:09

Michael Buen