Tech Off Post

Single Post Permalink

View Thread: How can I combine 2 ObservableCollections side by side?
  • Sathyaish Chakravarthy

    Hi,

    You can copy them to HashSet<T> and then do all the possible Set theory operations on them like Union, Intersection, etc. without writing the code for it yourself.

    HashSet<T> has a parameterized constructor that accepts an IEnumerable<T>, so you can copy the data easily.

    HashSet<Foo> hashSet1 = new HashSet<Foo>(myObservableCollection1);
    
    HashSet<Foo> hashSet2 = new HashSet<Foo>(myObservableCollection2);
    
    hashSet1.UnionWith(hashSet2);
    
    hashSet1.IntersectionWith(hashSet2);

     

    and so on.