收录日期:2021/01/28 17:13:48 时间:2012-06-17 04:28:08 标签:c#-2.0

How can I convert a DataRow array to a DataTable without iteration?

You can use:

dataTable = datarowarray.CopyToDataTable()

but ensure that datarowarray has length > 1, otherwise it will end up with unwanted exceptions.

I dont think you can put an array of datarows to a datatable. You can import one row at a time using DataTable.ImportRow.

foreach(DataRow row in dataRowArray)
{
   dataTable.ImportRow(row);
}

Even if there did exist a .NET framework function like

myDataTable.LoadRows(dataRowArray)

... all that would do is hide the iteration. The framework doesn't magically circumvent the iterative step (though in some cases it might be doing something gee-whiz smart to optimize it).

    DataTable dt = new DataTable();
    DataRow[] dataRowArray = dt.Select("");
    DataTable dataTable = new DataTable();
    foreach (DataRow row in dataRowArray)
    {
        dataTable = dataTable.Clone();
        dataTable.ImportRow(row);
    }