即使在RemoveAt()
方法,我的名單一直是相同的,我甚至不得到一個錯誤:RemoveAt移除()不工作C#
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()'。 – Arran
每次創建一個新列表('queryCandidates.ToList()。RemoveAt(i);'),從它中刪除一個項目並丟棄列表 –
空的catch {}'只會隱藏錯誤。最好刪除它。 –