Share via


Copy DataTable column items in a string array....

Question

Monday, April 14, 2008 5:02 AM

Hi All,

         I have a DataTable with 1 column. I want to copy each column item into string array without looping. Is there any way?  

Thanks,

ketan

All replies (5)

Monday, April 14, 2008 9:54 AM ✅Answered

you can use linq to avoid writing an explicit loop but in the background a loop will be run

 

True, but it is pretty well hidden (in Enumerable.ToArray<T>()).

Any copy of a collection must involve some looping (or recursion) somewhere.

 


Friday, April 18, 2008 9:06 AM ✅Answered

"Select(...)" in this case is the extension method from System.Linq.Enumerable class. 


Monday, April 14, 2008 8:51 AM

 You can use LINQ. Assuming the column is of type string and without nulls: 

theTable.Rows.Select(r => r[0]).ToArray()

 
You can add to the lambda to handle DB Nulls or type conversion.


Monday, April 14, 2008 9:31 AM

you can use linq to avoid writing an explicit loop but in the background a loop will be run


Friday, April 18, 2008 6:33 AM

Hi,

 Thanks for reply. But datatable.rows don't have select method. Can you please write a complete code.

Thanks,

Ketan