1

Let's say we have the "EntityCollection products".

Then the following doesn't work:

foreach (var product in products)
{
 product.LastUpdate = DateTime.Now();
}

As you can also not index an EntityCollection, how do you modify an entity in en EntityCollection?

Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273
user318554
  • 113
  • 1
  • 6
  • Can you please elaborate on "doesn't work?" – Craig Stuntz Apr 16 '10 at 15:22
  • Well, when executing Visual Studio complains that "products" is an enumerable collection, which you can't change during enumeration. Then, when I use "for (int i = 0; i < products.count;i++){product[i].LastUpdate = DateTime.Now();}" , it complains that "products" is not indexable... – user318554 Apr 16 '10 at 19:43
  • Can you show the code where products is declared / initialized. – Ian Mercer Nov 02 '10 at 01:58

2 Answers2

1

Try this, i create a generic method to update EntityCollection. Giv me your feed back.

You juste have to modify your product in your products entity collections. And call my GenericUpdateEntityCollection(YourEntityCollection, YourObjectContext); and there you go

Community
  • 1
  • 1
Cédric Boivin
  • 10,854
  • 13
  • 57
  • 98
0

Could you try this?

foreach (var product in products.ToArray())
{
     product.LastUpdate = DateTime.Now();
}

You have to copy elements for enumeration with ToArray(), so you can change products.

LukLed
  • 31,452
  • 17
  • 82
  • 107