Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datatable to Multidimensional Array

Is there an easy way to convert a Datatable to a multidimensional string array?

Maybe using LINQ?

There's gotta be a better way than manually looping through all the columns/rows...

like image 794
Greg Avatar asked Jan 19 '12 20:01

Greg


2 Answers

Linq is the answer. You can convert a DataTable to IEnumerable using the AsEnumerable method. Then, the ToArray() converts it to an array.

var tableEnumerable = DataTableName.AsEnumerable();
tableArray = tableEnumerable.ToArray();
like image 189
Gabriel GM Avatar answered Nov 16 '22 00:11

Gabriel GM


yourTable.AsEnumerable().Select(row => row.ItemArray).ToArray()

like image 4
user2782518 Avatar answered Nov 16 '22 01:11

user2782518