简体   繁体   English

仅获取列表中的最后一项

[英]Only getting last item in list

I'm trying to return all data from a list that has more than 30 items. 我正在尝试从具有30多个项目的列表中返回所有数据。 For some reason my code is only giving me the list's last item. 由于某种原因,我的代码只给了我列表的最后一项。 I'm sure this is a simple fix but if anyone could help me out that would be great. 我敢肯定这是一个简单的解决方法,但是如果有人可以帮助我,那将是很好的。 Here's the code. 这是代码。

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
        }
    }
}

You are overwriting your output file on each iteration. 您将在每次迭代中覆盖输出文件。 Place file opening outside the foreach. 将文件打开置于foreach之外。

using (StreamWriter write = new StreamWriter("PROPNUMTEST.txt"))
{
   foreach (string x in propnumList)
   {
     ....
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM