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?
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.
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(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With