Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将DataColumn值转换为字符串数组时的最佳实践?_C#_Arrays_String_Datatable - Fatal编程技术网

C# 将DataColumn值转换为字符串数组时的最佳实践?

C# 将DataColumn值转换为字符串数组时的最佳实践?,c#,arrays,string,datatable,C#,Arrays,String,Datatable,将DataColumn值转换为字符串数组时的最佳实践 [编辑] 要转换为字符串数组的所有DataTable行的特定DataColumn的所有值?DataRow.ItemArray属性- 另外,您使用的是哪个版本?您应该查看DataTableExtensions类- 以及DataRowExtensions类- 如果我理解了您的目标,您希望指定一个特定列,并将其所有值作为字符串数组返回 尝试以下方法: int columnIndex = 2; // desired column index /

将DataColumn值转换为字符串数组时的最佳实践

[编辑]
要转换为字符串数组的所有DataTable行的特定DataColumn的所有值?

DataRow.ItemArray属性-

另外,您使用的是哪个版本?您应该查看DataTableExtensions类-

以及DataRowExtensions类-


如果我理解了您的目标,您希望指定一个特定列,并将其所有值作为字符串数组返回

尝试以下方法:

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();
编辑:您特别要求使用字符串数组,但如果您对要求灵活,我希望使用
列表
,以避免在第一个示例中,在for循环之前确定数组长度,只需向其中添加项即可。这也是使用foreach循环的好机会

然后,我将重写代码,如下所示:

List<string> list = new List<string>();
foreach (DataRow row in dt.Rows)
{
    list.Add(row[columnIndex].ToString());
}
List List=新列表();
foreach(数据行中的数据行)
{
添加(行[columnIndex].ToString());
}

我知道这个问题很老了,但我在谷歌搜索中也发现了类似的问题。我想从datatable特定行中包含的所有值创建一个列表。在下面的代码示例中,我使用GUI向导向Visual Studio中的项目添加了一个SQL数据源,并将所需的表适配器放到设计器中

“创建专用数据表”
私有authTable作为新的qmgmtDataSet.AuthoritiesDataTable
'使用表适配器填充专用表
Me.AuthoritiesTableAdapter1.Fill(Me.authTable)
'创建值列表
Dim authNames As List(Of String)=新列表(Of String)(从Me.authTable.Rows中作为qmgmtDataSet.AuthoritiesRow的值选择names.authName)

您可以指定吗?任何Datarow都具有ItemArray属性,该属性以与为其定义的列相同的顺序返回对象数组。然后,将对象转换为字符串就像对每个值调用ToString()一样简单,但可能您希望…DataColumn的值转换为字符串数组,但我正在寻找实现这一点的最佳方法。
DataRow.ItemArray
将行中的所有值作为数组返回。我认为OP希望列中的所有值都作为数组。也许您可以扩展您的答案来帮助解释
DataRow.ItemArray
DataTableExtensions
类如何提供帮助?
List<string> list = new List<string>();
foreach (DataRow row in dt.Rows)
{
    list.Add(row[columnIndex].ToString());
}