-4

I want a copy of an ObservableCollection by filtering it with some condition. Please help me. I treid the following code:

ObservableCollection<YourEntity> clonedCollection = new ObservableCollection<YourEntity>(clonedList);

This is ok, but need copy with condition (new object filtered from the old one). need to copy some particular data from clonedList

FM Plus
  • 550
  • 1
  • 5
  • 12
  • Possible duplicate of [How create a new deep copy (clone) of a List?](http://stackoverflow.com/questions/14007405/how-create-a-new-deep-copy-clone-of-a-listt) – Sebastian P. Nov 27 '15 at 22:57

1 Answers1

0

Use a linq query to filer the elements in the source collection. A .Where() query does it pretty good. You can use .Select() or any other query too.

ObservableCollection<YourEntity> cloned = new ObservableCollection<YourEntity>(source.Where(e => e.IsSomeConditionFulfilled()));

Note that you do not clone the objects passing an IEnumerable to the constructor of the ObservableCollection - both collections cloned and source will reference the same objects. That means, if you change some YourEntity instance properties via the source collection (e. g. source.First().SomeProperty = SomeValue), and this object is also contained in the cloned collection (e. g. cloned.First() == source.First()), then those changes will be reflected there.

dymanoid
  • 14,771
  • 4
  • 36
  • 64
  • thanks for your quick reply. ok, I can understand. But i need to clone observableCollection (with reference of cloned collection). by passing some conditions to filter the cloned object, which is to be affected in both objects. – FM Plus Feb 06 '15 at 14:40