简体   繁体   中英

RemoveAt() not working c#

Even after the RemoveAt() method, my list keeps being the same and I don't even get an error:

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() creates a new instance. From this instance you are removing the element. You are not removing the element from the original enumerable.

You should be doing something like this instead:

var candidates = queryCandidates.ToList();
var elementsToRemove = new List<int>();
foreach (var row in candidates)
{
    // ...
    xString = candidates[i].District;
    // ...
    if (temp == false)
    {             
        // ... 
        elementsToRemove.Add(i);
    }  
}

for(int i = elementsToRemove.Count - 1; i >= 0; --i)
    candidates.RemoveAt(elementsToRemove[i]);

return View(candidates);

Please note the use of elementsToRemove . You can't remove the items directly in the loop. This will throw an exception.


Additionally, please note that ToList copies all data. Every single time you call it. It should be obvious that this is not a good idea to do in a loop.

queryCandidates.ToList().RemoveAt(i);

ToList() creates a brand new list, which you then remove an element from, but that list is long gone.

Try:

var newList = queryCandidates.ToList();

for (int i=newList.Count-1; i>=0; i--){
 ///snip
 newList.RemoveAt(i);

Note that I changed your foreach to for (in reverse) because you cannot modify a list while you are iterating over it with foreach.

The ToList() function creates a new List every time you call it. The object is removed from that list, not from the original list. So you should call ToList once before the foreach.

Once you've done that the removeAt() call will work and cause new issues because then you are trying to modify the list from within the foreach loop. So you'll need to rewrite your code in a way which takes the remove out of the loop as well.

Well I'm not exactly sure what Type queryCandidates is, but the reason you are not seeing an update is because you are removing element 'i' from the wrong object. Your ToList() function creates a new object of List type. If you want to keep the change you need to cache that list and use it where you use your original queryCandidates object.

queryCandidates isn't a list.

You're converting it to a list which creates a new instance from which you're removing the item but doesn't affect queryCandidates itself.

You can do:

var queryCandidates myCollection.ToList();

and then

queryCandidates.RemoveAt(i);

What works for me is to remove from the bottom up:

for (int i = list.Count - 1; i > 0; i--)
{
    if (list[i][0] == "&nbsp;" || list[i][3] == "0")
        list.RemoveAt(i);
}

It makes sense that some items are missed after decreasing the item count.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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