Hi I have an Object named Agency, which contains values for an agency including all of the contacts in a List format. This list would include the contacts details like name, age, address..
I am trying to loop through the list to return only one matching contact from the list, but run into a "Collection was modified; enumeration operation may not execute". When there is 2 contacts left in the list. I am unsure how to handle this, can someone shed a little light, thanks.
foreach (var x in lstOneContact)
{
foreach (var z in x.Contacts)
{
for (int i = 0; i <= x.Contacts.Count; i++)
{
if (z.ContactId != contactId)
{
x.Contacts.RemoveAt(i);
}
}
}
}