EntityCollection Issue

Posts   
 
    
jeffreygg
User
Posts: 805
Joined: 26-Oct-2003
# Posted on: 23-Nov-2003 05:53:40   

Anyone run into the following error message?

Message "Collection was modified; enumeration operation may not execute."   

This is generated from the .NET framework (mscorlib). Here's the situation:

I retrieve a collection from an entity as such:

EntityCollection = Entity.EntityCollection

or

EntityCollection = New EntityCollection

then add some entities to the collection as such:

EntityCollection.Add(NewEntity)

I then iterate through the collection and modify a property on each entity as such:

For each entity in EntityCollection
Entity.Property = Value
Next

The above error hits the first time "Next" is called. Any ideas?

jeffreygg
User
Posts: 805
Joined: 26-Oct-2003
# Posted on: 23-Nov-2003 06:00:13   

Just checked... in the iteration (For Each...Next) if I don't modify the property the error doesn't occur.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39855
Joined: 17-Aug-2003
# Posted on: 23-Nov-2003 10:23:08   

Will look into it. I do create an enumerator (albeit a default one), but it might be something with the Equals result which is performed over the entity values, and which can confuse the .NET code of foreach, don't know...

in the meantime, you can use the workaround with: Dim i As Integer For i = 0 to entityCollection.Count-1 entityCollection(i).Property = value Next

A little more work, however you can continue programming while I nail down this one simple_smile

Frans Bouma | Lead developer LLBLGen Pro
jeffreygg
User
Posts: 805
Joined: 26-Oct-2003
# Posted on: 24-Nov-2003 00:14:46   

Aye, that's the workaround I used. Thanks.

Jeff...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39855
Joined: 17-Aug-2003
# Posted on: 24-Nov-2003 09:20:52   

What's however much more efficient is the UpdateMulti() statement to update a range of entities directly in the persistent storage using a filter. This will construct an UPDATE statement which is much faster than the individual update statements when you call SaveMulti() on a collection with changed entities. Just a thought simple_smile

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39855
Joined: 17-Aug-2003
# Posted on: 24-Nov-2003 13:22:04   

You're running into the issue I reported today I think: new entities are not always added to the collection. I'm investigating this further with testcode.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39855
Joined: 17-Aug-2003
# Posted on: 24-Nov-2003 13:35:01   

C#'s foreach statement's help document has a note which says that you should not modify the contents of a collection with foreach. VB.NET however does not mention this, in fact, the documentation states that you can modify collection contents using For Each smile ... both simply use the enumerator object received. simple_smile

It seems the hashtable class has the same issues, at least in C#: when modifying a value inside a foreach, it throws the same error.

Looking further into it...

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39855
Joined: 17-Aug-2003
# Posted on: 24-Nov-2003 14:12:10   

I can't find the source for this problem. In C# it works fine when I do this: CustomerCollection allCustomers = new CustomerCollection(); allCustomers.GetMulti(null); foreach(CustomerEntity custo in allCustomers) { custo.Region = "Changed"; Console.WriteLine("Current region of {0} = {1}", custo.CustomerID, custo.Region); }

I also tested it in VB.NET: Dim allCustomers As New CustomerCollection

allCustomers.GetMulti(Nothing)

For Each customer As CustomerEntity In allCustomers customer.Region = "Changed" Console.WriteLine("New range of {0} = {1}", customer.CustomerID, customer.Region) Next

No error whatsoever, even when MS advices not to change anything inside the collection, since the Collection.GetEnumerator() enumerator which is returned is for readonly access (All IEnumerator implementations are).

This is with the fix for this error: http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=201 applied. (so I think OR it fixes it, OR I can't reproduce the error you have...).

I'll mail you the fixed ORM support classes library so you can try it out if it indeed fixes your problem. Let me know what the results are so I can set up further testing or release the new dll simple_smile

Frans Bouma | Lead developer LLBLGen Pro
jeffreygg
User
Posts: 805
Joined: 26-Oct-2003
# Posted on: 25-Nov-2003 22:48:16   

Great, I'll attempt to reproduce the error with the files you sent. I'll let you know. Thanks.

Jeff...