C# ADO.NET:将数据表转换为数据行数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2189658/
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
ADO.NET: convert a DataTable to an array of DataRows
提问by Craig Schwarze
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?
我正在使用 ADO.NET 和 C#,并且我想将一个 DataTable 对象转换为一个 DataRows 数组。这样做的优雅方式是什么?
采纳答案by Joel Etherton
My first question would be why? The request makes no sense.
我的第一个问题是为什么?请求没有意义。
The answer is:
答案是:
DataRow[] rows = myDataTable.Select();
回答by Guido Zanon
Actually the DataTable has a property called Rows, witch provides the methods to do this.
实际上 DataTable 有一个名为 Rows 的属性,女巫提供了执行此操作的方法。
You can accomplish this doing:
您可以完成此操作:
List<System.Data.DataRow> r = d.Rows.AsQueryable().OfType<System.Data.DataRow>().ToList();
回答by ksk
DataTable.Select() gives you an array of DataRows. You can use this as an array
DataTable.Select() 为您提供一个 DataRow 数组。您可以将其用作数组
Dim dt As New DataTable
Dim dr() As DataRow = dt.Select()
In case you want an ArrayList, you can
如果你想要一个 ArrayList,你可以
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.
我没有使用 dt.rows.CopyTo 函数。也许这也有效。
回答by David Stanley
If you would like to see the contents as a string, use this code:
如果您想以字符串形式查看内容,请使用以下代码:
string.Join(",", dataTable.AsEnumerable().SelectMany(row => row.ItemArray))