C# 将数据行数组转换为数据表的简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2122511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple way to convert datarow array to datatable
提问by Developer404
I want to convert a DataRow
array into DataTable
... What is the simplest way to do this?
我想将DataRow
数组转换为DataTable
......最简单的方法是什么?
采纳答案by Jay Riggs
Why not iterate through your DataRow array and add (using DataRow.ImportRow, if necessary, to get a copy of the DataRow), something like:
为什么不遍历您的 DataRow 数组并添加(使用 DataRow.ImportRow,如有必要,获取 DataRow 的副本),例如:
foreach (DataRow row in rowArray) {
dataTable.ImportRow(row);
}
Make sure your dataTable has the same schema as the DataRows in your DataRow array.
确保您的 dataTable 与 DataRow 数组中的 DataRows 具有相同的架构。
回答by Mitch Wheat
DataTable dt = new DataTable();
DataRow[] dr = (DataTable)dsData.Tables[0].Select("Some Criteria");
dt.Rows.Add(dr);
回答by joe
For .Net Framework 3.5+
对于 .Net 框架 3.5+
DataTable dt = new DataTable();
DataRow[] dr = dt.Select("Your string");
DataTable dt1 = dr.CopyToDataTable();
But if there is no rows in the array, it can cause the errors such as The source contains no DataRows. Therefore, if you decide to use this method CopyToDataTable()
, you should check the array to know it has datarows or not.
但是如果数组中没有行,则会导致诸如The source contains no DataRows 之类的错误。因此,如果您决定使用此方法CopyToDataTable()
,您应该检查数组以了解它是否有数据行。
if (dr.Length > 0)
DataTable dt1 = dr.CopyToDataTable();
Reference available at MSDN: DataTableExtensions.CopyToDataTable Method (IEnumerable)
MSDN 上提供的参考: DataTableExtensions.CopyToDataTable Method (IEnumerable)
回答by zaib shah
Here is the solution. It should work fine.
这是解决方案。它应该可以正常工作。
DataTable dt = new DataTable();
dt = dsData.Tables[0].Clone();
DataRows[] drResults = dsData.Tables[0].Select("ColName = 'criteria');
foreach(DataRow dr in drResults)
{
object[] row = dr.ItemArray;
dt.Rows.Add(row);
}
回答by ilans
Another way is to use a DataView
另一种方法是使用 DataView
// Create a DataTable
DataTable table = new DataTable()
...
// Filter and Sort expressions
string expression = "[Birth Year] >= 1983";
string sortOrder = "[Birth Year] ASC";
// Create a DataView using the table as its source and the filter and sort expressions
DataView dv = new DataView(table, expression, sortOrder, DataViewRowState.CurrentRows);
// Convert the DataView to a DataTable
DataTable new_table = dv.ToTable("NewTableName");
回答by Miriam
DataTable dt = myDataRowCollection.CopyToDataTable<DataRow>();
回答by user1036202
DataTable dt = new DataTable();
foreach (DataRow dr in drResults)
{
dt.ImportRow(dr);
}
回答by Zolfaghari
Simple way is:
简单的方法是:
// dtData is DataTable that contain data
DataTable dt = dtData.Select("Condition=1").CopyToDataTable();
// or existing typed DataTable dt
dt.Merge(dtData.Select("Condition=1").CopyToDataTable());
回答by logixologist
Incase anyone needs it in VB.NET:
如果有人在 VB.NET 中需要它:
Dim dataRow as DataRow
Dim yourNewDataTable as new datatable
For Each dataRow In yourArray
yourNewDataTable.ImportRow(dataRow)
Next
回答by Haseeb Mukhtar
.Net 3.5+ added DataTableExtensions, use DataTableExtensions.CopyToDataTable Method
.Net 3.5+ 添加了 DataTableExtensions,使用 DataTableExtensions.CopyToDataTable 方法
For datarow array just use .CopyToDataTable() and it will return datatable.
对于数据行数组,只需使用 .CopyToDataTable() 它将返回数据表。
For single datarow use
对于单个数据行使用
new DataRow[] { myDataRow }.CopyToDataTable()