我正在尝试从包含超过30个项目的列表中返回所有数据。出于某种原因,我的代码只给了我列表的最后一项。我确信这是一个简单的解决办法,但如果有人能帮助我,那就太棒了。这是代码。
List<string> propnumList = new List<string>();
foreach (DataRow drRow in ds7.Tables[0].Rows)
{
for (int i = 0; i < ds7.Tables[0].Columns.Count; i++)
{
propnumList.Add(drRow[i].ToString());
}
}
using (StreamWriter sw = new StreamWriter("propnumList.txt"))
{
foreach (string s in propnumList)
{
sw.WriteLine(s);
}
}
string tempProp = "";
foreach (string x in propnumList)
{
if (x.Length < 30)
{
x.Equals(null);
}
else
{
tempProp = x.Substring(31);
using (StreamWriter write = new StreamWriter("PROPNUMTEST.txt"))
{
write.WriteLine(tempProp); WANT TO RETURN MORE THAN JUST LAST ITEM
}
}
}
答案 0 :(得分:15)
您将在每次迭代时覆盖输出文件。将文件打开放在foreach之外。
using (StreamWriter write = new StreamWriter("PROPNUMTEST.txt"))
{
foreach (string x in propnumList)
{
....
}
}