How to copy observable collection

Relativity picture Relativity · Nov 14, 2010 · Viewed 34.4k times · Source

I have

Observablecollection<A> aRef = new Observablecollection<A>();
bRef = aRef(); 

In this case both point to same ObservableCollection... How do I make a different copy?

Answer

Aliostad picture Aliostad · Nov 14, 2010

Do this:

// aRef being an Observablecollection 
Observablecollection<Entity> bRef = new Observablecollection<Entity>(aRef);

This will create an observable collection but the items are still pointing to the original items. If you need the items to point a clone rather than the original items, you need to implement and then call a cloning method.

UPDATE

If you try to add to a list and then the observable collection have the original list, just create the Observablecollection by passing the original list:

List<Entity> originalEnityList = GetThatOriginalEnityListFromSomewhere();
Observablecollection<Entity> bRef = new Observablecollection<Entity>(originalEnityList);