4
EntityCollection.ToList().Clear()

Does not clear the entity collection. Any idea why?

Any solution?

How should i clear the EntityCollection?

mattytommo
  • 52,879
  • 15
  • 115
  • 143
ove
  • 2,814
  • 5
  • 28
  • 48

2 Answers2

4

Because ToList() creates a copy of the EntityCollection as a List<T> and then you just clear that list and not the EntityCollection itself.

Edit 1: Use the Clear() method from EntityCollection: http://msdn.microsoft.com/de-de/library/bb302707.aspx

Edit 2: Oh I see. So it's this class: http://msdn.microsoft.com/de-de/library/ff422654(v=vs.91).aspx ? Seems you have to enumerate all items and delete them one by one.

foreach( var item in EntityCollection.ToList() )
    EntityCollection.Remove(item);

Here you need ToList() to create a copy because most of the collection classes don't like it when you delete items from them during enumeration.

mattytommo
  • 52,879
  • 15
  • 115
  • 143
Torben Schramme
  • 1,888
  • 1
  • 14
  • 23
  • EntityCollection does not have Clear() – ove May 02 '12 at 05:50
  • As I said with the Clear() method from EntityCollection. Just postet the msdn link. – Torben Schramme May 02 '12 at 05:50
  • The EntityCollection is part of this namespace: System.ServiceModel.DomainServices.Client.Entity .. DO not have Clear() – ove May 02 '12 at 05:53
  • Then it's not the ADO.NET EF although the classes have the same name and simelar members. Think about retagging your question with http://stackoverflow.com/questions/tagged/wcf-ria-services – Torben Schramme May 02 '12 at 06:10
0

ToList() generates a new list (you are clearing that).

ChristopheD
  • 100,699
  • 26
  • 154
  • 173