CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2005
    Posts
    4

    “Collection was modified; enumeration operation might not execute” inside System.Data

    I have an .NET 4.0 assembly; it is registered in GAC and works as part of a BizTalk “orchestration”.
    Sometimes I get the following error - “Collection was modified; enumeration operation might not execute. : System.InvalidOperationException: Collection was modified; enumeration operation might not execute.”. I cannot reproduce it; when I run the same processing of the same data, my assembly does not generate the error in this place.

    The error happens when I call ‘.Where(<condition>).ToArray()’ for a datatable object: an object of classd System.Data.TypedTableBase<MyDataRowClass>.

    Here is the code:
    ..................
    int? setTypeGroupId;
    ...

    return instances.WorkContributors.Where
    (
    c =>
    !c.IsInterestedPartyNoNull()
    && c.InterestedPartyNo == publisherIpNo
    && c.SetTypeNo == 1
    && (c.RecordType == "SPU")
    && c.TypeCode == "E"
    && (!setTypeGroupId.HasValue ||
    (setTypeGroupId.HasValue && c.SetTypeGroupID == setTypeGroupId))
    )
    .ToArray();
    ..................

    The object ‘instances’ is a dataset – my class produced from System.Data.DataSet.
    The property ‘instances.WorkContributors’ is a datatable: an object of class System.Data.TypedTableBase<MyDataRowClass>.
    The class MyDataRowClass is produced from System.Data.DataRow.

    The call stack after the error was the following:
    Collection was modified; enumeration operation might not execute. : System.InvalidOperationException: Collection was modified; enumeration operation might not execute.
    at System.Data.RBTree`1.RBTreeEnumerator.MoveNext()
    at System.Linq.Enumerable.<CastIterator>d__97`1.MoveNext()
    at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
    at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
    at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
    at MyProduct.FileParser.Types.CWR.PWRType.GetPublishers(CWRWorkInstances instances, Nullable`1 setTypeGroupId)
    at MyProduct.FileParser.Validation.Concreate.PwrTypeValidation.ValidatePublisherNumber()
    at MyProduct.FileParser.Validation.Concreate.PwrTypeValidation.Validate()
    at MyProduct.FileParser.Types.CWR.PWRType.StoreRecord(CWRWorkInstances workInstances, CWRWorkParsingContext context)
    at MyProduct.FileParser.Groups.CWR.NWRGroup.StoreGroup(Int32 workBatchID, CWRFileCommonData commonData)
    at MyProduct.FileParser.CWRParser.ProcessCWRFile(String fileName, Boolean wait, Boolean deleteFile, String sourceFileName)


    I cannot understand why the error happens; and why it happens only sometimes and does not happen on the same processed data again.
    The error “Collection was modified; enumeration operation might not execute.” Itself is pretty straightforward for me; but I do not see why it happens in that my code. The error is excepted if a code like this:

    foreach (DataRow currRow in _someDataTable.Rows)
    {
    if (/*deletion condition*/)
    {
    someDataTable.Rows.Remove(currRow);
    }
    }
    But my code above just wants to enumerate System.Data.TypedTableBase<MyDataRowClass> and convert the result into an array.

    Any ideas?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: “Collection was modified; enumeration operation might not execute” inside System.

    You cannot delete from a collection as you are enumerating through the collection.

    One workaround is to put the items you wish to delete into a temp list as you walk through the collection and the walk the list afterwards to remove each item from the original collection.

    Code:
    var itemsToDelete = new List<DataRow>();
    
    foreach (DataRow currRow in _someDataTable.Rows)
    {
        if (/*deletion condition*/)
        {
            itemsToDelete.Add(currRow);
        }
    }
    
    foreach(var item in itemsToDelete)
    {
      _someDataTable.Rows.Remove(item);
    }

  3. #3
    Join Date
    Apr 2019
    Posts
    4

    Re: “Collection was modified; enumeration operation might not execute” inside System.

    Arjay the OP knows that, the second code is not what he's asking... have you read his question?

    Anyway, VictorSotnikov can you show me how your WorkContributors property looks like?
    Also, can you check who else is using WorkContributors property, is it possible that someone is accessing/modifying it in different thread?

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: “Collection was modified; enumeration operation might not execute” inside System.

    Quote Originally Posted by Nountylegre View Post
    Arjay the OP knows that, the second code is not what he's asking... have you read his question?
    The code I'm referring to will have an issue when deleting while enumerating the collection as that was the error. At any rate, how do you know what I've read or what the OP knows? Perhaps it would be best for you to simply focus on trying to answer the question?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured