Tech Off Thread

6 posts

How can I combine 2 ObservableCollections side by side?

Back to Forum: Tech Off
  • Blunderbore

    I have a lot of different ObservableCollections.  They all have the same number of elements and grow together as new data arrives.  They have different properties and I would like to be able to combine them together for use in charting and several datagrids using databinding.  Is there an easy way to do this without coping all of the data to new ObservableCollections.  The user will be able to select which ones they want to compare and bring them up for viewing.

    Thanks

  • 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.

  • Blunderbore

    Thanks for your response.  But I do not think HashSet will work for me.  I have provided additional info below.

    I have done a lot of studying on this and I believe that I now have a better understanding of what I need and how I should ask the question.  Although I have many, for the sake of simplicity I will only refer to 2 classes here as an example.  I am gathering data in real time.  The data is being processed and placed into ObservableCollection classes.  All classes will have the same number of data points.  Any class can have many duplicates.  The order of the data must not change.  I am looking for a way to somehow merge multiple classes together side by side so the resulting class can be used as a single data source with all properties combined when data binding to a WPF chart or datagrid.  I would like to do this without have to copy all of the data to the new object, something like a data view.  Here's what I mean by "merge multiple classes together side by side". 

    Starting with 2 ObservableCollection classes.  The first ObservableCollection is of objects with properties A, B and C and the second ObservableCollection is of objects with properties D and E.  I would like to be able to combine them into a single data source of A, B, C, D and E.  Hopefully as some kind of data view where the underlining data is accessed but not copied.  It seems that CompositeCollection just puts them on top of each other.

  • Dr Herbie

    @Blunderbore: I don't think there's a built-in mechanism for this.

    Because you store the data in classes (rather than in primitives like int or double), then when you 'copy' the data to a new collection, you are not actually copying the whole data, only a reference to the data; this means that copying is less of a problem.

    If you want to combine the contents of two separate classes into a single source, then you can create a 'wrapper' class that takes two objects in it's constructor and exposes their properties as it's own. e,g,:

     

    public class Wrapper
    {
        private ClassA _a;
        private ClassB _b;
    
        public Wrapper(ClassA a, ClassB b)
        {
            _a = a;
            _b = b;
        }
    
        public double A
        {
            get
            {
                return _a.A;
            }
            set
            {
                _a.A = value;
            }
        }
    
        // ... also for properties B, C & D
    
        public double E
        {
            get
            {
                return _b.E;
            }
            set
            {
                _b.E = value;
            }
        }
    }

     

    This means you're not duplicating the original data (it's still sits in the original classes) but you can treat the wrapper class as a combined view.

    However, you are going to have to manually create a new collection of these wrapper classes to present the new data for databinding.

     

    Herbie

     

     

     

     

  • Blunderbore

    Thanks Herbie for your response.  I have tried this and many other permutations.  I have been unable to get the Wrapper class to look and act like a single ObservableCollection to the rest of the system.  Change notifications did not seem to get through the Wrapper and the pointer to which object within the ObservableCollections was not staying in sync.  I tried all kinds of things adding band aids to the point that it was so convoluted that I could no longer follow what was happening.  It seemed to me that as extensive as the .NET framework is that this feature would be there somewhere.  I just did not know what to call it or where to look.  So I asked this question.  Maybe I'm the only one that needs to bring data together like.

    Any additional help or ideas would be greatly appreciated.

    Blunderbore

  • Adam​Speight2008

    What about using zip.

    col_A.Zip(col_B, Function(a,b) {a,b})

    or using RX?

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.