2列数据表到列表< int> .NET 2.0 [英] 2-Column DataTable to List<int> .NET 2.0
问题描述
$ b
C#在.NET 2.0 / Visual Studio 2005中编写的旧版Web应用程序中已存储了一个DataTable。
$ b
我正在使用DataTable中的值填充列表,但是我仍然继续处理几个问题。
我的转换过程如下所示:
列表< int> SpecialVendorList = new List< int>();
foreach(GetAllSpecialVendors()中的DataRow数据行)
{
//循环遍历每行
foreach(GetAllSpecialVendors()中的DataColumn列列)
{
SpecialVendorList.Add(column [ChildVendorId]);
SpecialVendorList.Add(column [ParentVendorId]);
}
}
这给我以下错误:
无法使用[]将索引应用于System.Data.DataColumn类型的表达式
对于每个SpecialVendorList.Add()方法。
似乎正在尝试获取每行的列值。你只有第一个 foreach
循环:
列表< int> SpecialVendorList = new List< int>();
/ pre>
尝试
{
foreach(GetAllSpecialVendors()中的DataRow数据行)
{
//循环遍历每行
SpecialVendorList。 Add(Convert.ToInt32(datarow [ChildVendorId]));
SpecialVendorList.Add(Convert.ToInt32(datarow [ParentVendorId]));
}
}
catch(FormatException fe)
{
//处理错误
}
此处的字符串索引将在该特定行中获取该列的值
I have populated a DataTable from a stored procedure in an older web application written in
C# under .NET 2.0 / Visual Studio 2005.
I'm trying to populate a List with the values in the DataTable, but I keep running up against a couple issues.
My conversion process looks like this:
List<int> SpecialVendorList = new List<int>(); foreach (DataRow datarow in GetAllSpecialVendors().Rows) { //Loop through each row foreach (DataColumn column in GetAllSpecialVendors().Columns) { SpecialVendorList.Add(column["ChildVendorId"]); SpecialVendorList.Add(column["ParentVendorId"]); } }
which gives me the following error:
Can not apply indexing with [] to an expression of type 'System.Data.DataColumn'
for each of the SpecialVendorList.Add() methods.
解决方案Seems like you're trying to get column values for each row. You only the first
foreach
loop:List<int> SpecialVendorList = new List<int>(); try { foreach (DataRow datarow in GetAllSpecialVendors().Rows) { //Loop through each row SpecialVendorList.Add(Convert.ToInt32(datarow["ChildVendorId"])); SpecialVendorList.Add(Convert.ToInt32(datarow["ParentVendorId"])); } } catch(FormatException fe) { //handle the error }
The string index here will get that column's value in that specific row
这篇关于2列数据表到列表< int> .NET 2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!