Problem throwing this Exception:
System.InvalidOperationException: 'Collection was modified; enumeration operation may not execute.'
foreach(var string in list)
{ if condition true;
remove element from list;
work for remaining list items; // Exception
Do anyone of you have solution to this issue?
Don't want answer like:
move items into new list and then remove all together. use for or something else
Dummy Code:
public List<Tuple<int, string, string>> OpM(List<Tuple<int, string, string>> tuple)
{
foreach (var v in tuple)
{
cal = (swl * 100) / iwl;
if (cal <= sf)
{
st = st + " " + v.Item3;
Console.WriteLine("Added: {0}", st);
tuple.Remove(v);
}
else {continue;}
}
}
return tuple;
}
THANK YOU SO MUCH! I got the answer.
foreach (var v in tuple.ToList())
{ tuple.Remove(v); }
But I also want to know that how to use these two way:
1- List.Distinct()
2- LINQ: list = list.Where(a => !condition);