开发者

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

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?


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

The answer is:

DataRow[] rows = myDataTable.Select();


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


DataTable.Select() gives you an array of DataRows. You can use this as an array

Dim dt As New DataTable
Dim dr() As DataRow = dt.Select() 

In case you want an ArrayList, you can

public ArrayList ConvertDT(ref DataTable dt)
{
        ArrayList converted = new ArrayList(dt.Rows.Count);
        foreach (DataRow row in dt.Rows)
        {
                converted.Add(row);
        }
        return converted;
}

I have not used the dt.rows.CopyTo function. maybe that works also.


If you would like to see the contents as a string, use this code:

string.Join(",", dataTable.AsEnumerable().SelectMany(row => row.ItemArray))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜