将DataColumn值转换为字符串数组时的最佳做法?

时间:2021-05-26 21:14:14

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?

[编辑]某些DataColumn的所有值,以便将所有DataTable行转换为字符串数组?

3 个解决方案

#1


22  

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:

您可以使用columnName替换columnIndex,例如:

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.

编辑:你已经特别要求一个字符串数组,但是如果你对这些要求很灵活,我宁愿使用List 来避免在第一个例子中在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());
}

#2


1  

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

#3


0  

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)

#1


22  

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:

您可以使用columnName替换columnIndex,例如:

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.

编辑:你已经特别要求一个字符串数组,但是如果你对这些要求很灵活,我宁愿使用List 来避免在第一个例子中在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());
}

#2


1  

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

#3


0  

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)