15

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

Yagiz Ozturk
  • 5,408
  • 7
  • 29
  • 44

2 Answers2

32
string[] array = yourTable
                 .AsEnumerable()
                 .Select(row => row.Field<string>("ColumnName"))
                 .ToArray();
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
5

You could do something like:

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

James Michael Hare
  • 37,767
  • 9
  • 73
  • 83
  • I was going to pick the selected comment code but then i was too lazy to check the column's name in the database so im going with this one :D – GabrielBB Mar 30 '15 at 14:38