C# 将 DataColumn 值转换为字符串数组时的最佳实践?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2077592/
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
Best practice when converting DataColumn values to an array of strings?
提问by Ahmed Atia
Best practice when converting DataColumn values to an array of strings?
将 DataColumn 值转换为字符串数组时的最佳实践?
[Edit]All values for certain DataColumn for all DataTable rows to be converted to an array of string?
[编辑]要转换为字符串数组的所有 DataTable 行的某些 DataColumn 的所有值?
采纳答案by Ahmad Mageed
If I understood your goal you want to specify a particular column and return all its values as a string array.
如果我理解您的目标,您想指定一个特定的列并将其所有值作为字符串数组返回。
Try these approaches out:
试试这些方法:
int columnIndex = 2; // desired column index
// for loop approach
string[] results = new string[dt.Rows.Count];
for (int index = 0; index < dt.Rows.Count; index++)
{
results[index] = dt.Rows[index][columnIndex].ToString();
}
// LINQ
var result = dt.Rows.Cast<DataRow>()
.Select(row => row[columnIndex].ToString())
.ToArray();
You could replace columnIndex
with columnName
instead, for example:
你可以替换columnIndex
与columnName
替代,例如:
string columnName = "OrderId";"
EDIT:you've asked for a string array specifically but in case you're flexible about the requirements I would prefer a List<string>
to avoid the need to determine the array length prior to the for loop in the first example and simply add items to it. It's also a good opportunity to use a foreach loop instead.
编辑:您已经特别要求一个字符串数组,但如果您对要求很灵活,我更喜欢 aList<string>
以避免需要在第一个示例中的 for 循环之前确定数组长度并简单地向其中添加项目。这也是使用 foreach 循环的好机会。
I would then rewrite the code as follows:
然后我将代码重写如下:
List<string> list = new List<string>();
foreach (DataRow row in dt.Rows)
{
list.Add(row[columnIndex].ToString());
}
回答by Kris Krause
DataRow.ItemArray Property -
DataRow.ItemArray 属性 -
http://msdn.microsoft.com/en-us/library/system.data.datarow.itemarray.aspx
http://msdn.microsoft.com/en-us/library/system.data.datarow.itemarray.aspx
Also, which version are you using? You should check out the DataTableExtensions class -
另外,你用的是哪个版本?您应该查看 DataTableExtensions 类 -
http://msdn.microsoft.com/en-us/library/system.data.datatableextensions.aspx
http://msdn.microsoft.com/en-us/library/system.data.datatableextensions.aspx
And the DataRowExtensions class -
和 DataRowExtensions 类 -
http://msdn.microsoft.com/en-us/library/system.data.datarowextensions.aspx
http://msdn.microsoft.com/en-us/library/system.data.datarowextensions.aspx
回答by в?a???? в?н???
I know this question is old, but I found it in my Google search trying to do something similar. I wanted to create a list from all the values contained in a specific row of my datatable. In my code example below, I added a SQL datasource to my project in Visual Studio using the GUI wizards and I dropped the needed table adapter into the designer.
我知道这个问题很老,但我在 Google 搜索中发现它试图做类似的事情。我想根据数据表特定行中包含的所有值创建一个列表。在下面的代码示例中,我使用 GUI 向导向 Visual Studio 中的项目添加了一个 SQL 数据源,并将所需的表适配器放入设计器中。
'Create a private DataTable
Private authTable As New qmgmtDataSet.AuthoritiesDataTable
'Fill the private table using the table adapter
Me.AuthoritiesTableAdapter1.Fill(Me.authTable)
'Make the list of values
Dim authNames As List(Of String) = New List(Of String)(From value As qmgmtDataSet.AuthoritiesRow In Me.authTable.Rows Select names.authName)