【发布时间】:2012-10-07 22:27:20
【问题描述】:
即使在RemoveAt() 方法之后,我的列表仍然保持不变,我什至没有收到错误:
foreach (var row in queryCandidates.ToList())
{
try
{
xString = queryCandidates.ToList().ElementAt(i).District;
int.TryParse(xString, out xNumber);
temp = xNumber.Equals(districtNumber);
System.Diagnostics.Debug.Write(temp+ " ");
System.Diagnostics.Debug.Write(i+" ");
if (temp == false)
{
System.Diagnostics.Debug.WriteLine(" i is:"+i);
//not working even when it should
queryCandidates.ToList().RemoveAt(i);
}
}
catch { }
i++;
if (last == i)
{
System.Diagnostics.Debug.WriteLine("before ending loop: ");
return View(queryCandidates.ToList());
}
}
System.Diagnostics.Debug.WriteLine("after ending the loop: ");
return View(queryCandidates.ToList());
【问题讨论】:
-
那里有很多
ToList()'ing。 -
每次创建新列表(
queryCandidates.ToList().RemoveAt(i);) 时,从中删除一个项目并丢弃该列表 -
空的
catch{}只会隐藏错误。最好删除它。
标签: c# list removeclass removeall