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

2 Answers 2

32
string[] array = yourTable
                 .AsEnumerable()
                 .Select(row => row.Field<string>("ColumnName"))
                 .ToArray();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Alot, i guess you forgot parantheses after .AsEnumarable
@Yagiz, indeed, I did. Was typing it directly in. Thanks for pointing it out.
5

You could do something like:

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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.