Search code examples
silverlightwcf-ria-servicesenumeration

Delete Records in Wcf Ria


I use wcf ria in silverlight application. in the client side, i use following code to delete some records:

    var installments = context.Installments.Where(i => i.RequestId == selectedRequest.RequestId);
    foreach (var installment in installments)
    {
        context.Installments.Remove(installment);
    }
    context.SubmitChanges();

when run this code i have following error:

Collection was modified; enumeration operation may not execute.

how can i fix this problem?!!


Solution

  • That's happening because you're removing objects from the collection, while you're enumerating it. The easiest way I've found to work around that is to add .ToArray() or .ToList() to the end of your enumeration line. I.e.

    var installments = context.Installments.Where(i => i.RequestId == selectedRequest.RequestId).ToArray();
    

    This will give you an enumeration that is "detached" from the original collection.

    Hope this helps. Nate