So, I have a bug to remove
foreach (XElement x in items.Elements("x")) { XElement result = webservice.method(x); if (/*condition based on values in result*/) { x.Remove(); } }
The problem is that calling x.Remove() alters the foreach such that if there are two Elements("x"), and the first is removed, the loop doesn't get to the second x element.
So how should I be looping this? Or should this be rewritten another way?
Use unset() function to remove array elements in a foreach loop. The unset() function is an inbuilt function in PHP which is used to unset a specified variable.
Unlike the for loop, the iteration variable in a foreach loop is read-only. Attempting to assign a value to the variable within the body of the loop will result in a compile-time error.
I suspect that Linq may be able to help you out here as follows.
using System.Linq; void foo() { items.Elements("x") .Where(x => condition(webservice.method(x))) .Remove(); }
If that doesn't work (i.e. the internal enumerator is still invalidated), make a shallow copy of the selected elements and delete them as follows.
using System.Linq; void foo() { List xElements = items.Elements("x") .Where(x => condition(webservice.method(x))) .ToList(); for (int i = xElements.Count - 1; i > -1; i--) { xElements[i].Remove(); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With