Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTable's Row's First Column to String Array

I have a DataTable. I want to get every rows first column value and append to a string array. I do not want to use foreach looping for every row and adding to string array. I tried this, but stuck at some point

DataRow[] dr = new DataRow[dtCampaignSubscriberLists.Rows.Count];
dtCampaignSubscriberLists.Rows.CopyTo(dr, 0);
string[] array = Array.ConvertAll(dr, new Converter<DataRow, String>(????));

Thanks

like image 656
Yagiz Ozturk Avatar asked May 25 '11 15:05

Yagiz Ozturk


2 Answers

string[] array = yourTable
                 .AsEnumerable()
                 .Select(row => row.Field<string>("ColumnName"))
                 .ToArray();
like image 185
Anthony Pegram Avatar answered Oct 07 '22 23:10

Anthony Pegram


You could do something like:

dtCampaignSubscriberLists.AsEnumerable().Select(r => r[0].ToString()).ToArray();

like image 44
James Michael Hare Avatar answered Oct 07 '22 22:10

James Michael Hare