MKBender

I have a sortedList, and I need to iterate through it, comparing it to a table with the updated data, and remove old items, what is the best way to do this I think what I have below will though an exception since I'm removing an item from the list that im iterating through, so how do I do this should I use a regular for loop instead

bool FOUND;
foreach (Equipment tmpEQ in equipments)//remove equipment no longer active
{
FOUND = false;
foreach (DataRow dr in EQTable.Rows)
{
if (dr["EQUIPMENT"].ToString() == tmpEQ.equipment)
FOUND = true;
}
if (FOUND == false)
equipments.Remove(tmpEQ);
}




Re: Visual C# General remove an item from a list while iterating through it

Wiggly

to be honest I am not sure you would get an exception.. it is possible (not tested it)... would suggest creating a very basic console app and feed it a fake list and test it out. if you need to go through each one the foreach seems like a good choice to me.

GL






Re: Visual C# General remove an item from a list while iterating through it

OmegaMan

Setup a side remove list that will contain the items to be removed. When you encounter one to be removed in the original foreach loop, add it to the remove list. Once outside the original foreach, enumerate over the remove list and remove the objects from the original list at that time.





Re: Visual C# General remove an item from a list while iterating through it

nielsvanvliet

You can use store in hashtable the ones that should be removed, then to remove them:


Code Snippet
Hashtable toRemove = new Hashtable();
bool FOUND;
foreach (Equipment tmpEQ in equipments)
if (!toRemove.Contains(tmpEQ))
{
FOUND = false;
foreach (DataRow dr in EQTable.Rows)
if (dr["EQUIPMENT"].ToString() == tmpEQ.equipment)
{ FOUND = true; break; }
if (!FOUND)
toRemove.Add(tmpEQ, null);
}
foreach (Equipment tmpEQ in toRemove)
equipments.Remove(tmpEQ);






Re: Visual C# General remove an item from a list while iterating through it

IsshouFuuraibou

the better way to do this is:

Code Snippet


Equipment ToRemove = null;
foreach (Equipment tmpEQ in equipments)//remove equipment no longer active
{
foreach (DataRow dr in EQTable.Rows)
{
if (dr["EQUIPMENT"].ToString() != tmpEQ.equipment)
{
ToRemove = tmpEQ;
break;
}
}
if ( ToRemove != null )
break;
}

if ( ToRemove != null)
equipments.Remove( ToRemove );

You do want to avoid removing while inside enumeration (foreach) because you're modifying the list, and that just causes problems with enumerating





Re: Visual C# General remove an item from a list while iterating through it

RWF

The reason you are generating an exception is because the foreach locks the enumerator of the Rows collection. When the enumerator is locked, you can't add/remove items from the collection. Depending how many rows you have, it may be faster to code it like this:

DataRow[] dRows = EQTable.Select(string.Format("Equipment = '{0}'", tmpEQ.equipment);

foreach(DataRow dr in dRows)
{
equipments.Remove(dr);
}

OR change the foreach above to:

foreach(DataRow dr in dRows)
{
dr.Delete();
}


edit: Just as an aside, it maybe faster to use the Select statement than a foreach because if you perform this action, the datatable creates an index. If you have 100,000 rows, it will surely be faster than a foreach, but if you have 50, go with whats easiest.




Re: Visual C# General remove an item from a list while iterating through it

dotnetideas

Do it in the reversed order, i.e from bottom to top.

for(int i = equipments.count-1; i>=0; i--)

{

Equipment tmpEQ = equipments[ i ];

.....

equipments.Remove(tmpEQ);

}






Re: Visual C# General remove an item from a list while iterating through it

boban.s

Just change instead of using foreach to use some other loop type, for example for.