Greg
Greg

Reputation: 8784

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...

Upvotes: 7

Views: 18349

Answers (3)

user2782518
user2782518

Reputation: 41

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

Upvotes: 4

Gabriel GM
Gabriel GM

Reputation: 6649

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();

Upvotes: 8

Gavin Fang
Gavin Fang

Reputation: 367

try dt.Rows.Cast().Select(//datarow to strings)

Upvotes: 0

Related Questions