Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ADO.NET: convert a DataTable to an array of DataRows

Tags:

c#

ado.net

I'm using ADO.NET and C#, and I want to convert a DataTable object into an array of DataRows. What is an elegant way of doing this?

like image 851
Craig Schwarze Avatar asked Feb 03 '10 04:02

Craig Schwarze


2 Answers

My first question would be why? The request makes no sense.

The answer is:

DataRow[] rows = myDataTable.Select();
like image 100
Joel Etherton Avatar answered Oct 23 '22 15:10

Joel Etherton


Actually the DataTable has a property called Rows, witch provides the methods to do this.

You can accomplish this doing:

List<System.Data.DataRow> r = d.Rows.AsQueryable().OfType<System.Data.DataRow>().ToList();
like image 37
Guido Zanon Avatar answered Oct 23 '22 15:10

Guido Zanon