I'll describe my object model, then what I want to do.
It is a Silverlight application, and these are model objects that are bound to UI elements.
An Agreement has a collection of TradingBranch, branches can be added or removed. A branch has collection of Product.
agreement.Branches
.SelectMany(x => x.Products)
.Distinct()
These collections are driven by a matrix of branches and products. The same products can be selected 开发者_StackOverflow中文版by more than one branch, hence the Distinct.
Essentially I want to let the user select from a list of all the products that have been selected as available for any of the branches. I want this list to be updated when there is a change in the matrix.
So rather than having to add a CollectionChanged handler for the branches, then more handlers to listen on the Products collection, work out whether the product is already there and then have to unsubscribe when branches are removed etc etc, I was hoping there was some nice Rx syntax I could employ to say simply - "listen to this piece of LINQ" and update this other observable collection that I'm binding my ListBox to when it changes.
Despite the name similarities, IObservable and ObservableCollection are completely unrelated and unfortunately also incompatible. They have two very different models of observing a collection.
Have a look at Bindable LINQ. It attempts to define LINQ-to-ObservableCollection, such that a LINQ query on a ObservableCollection results in a ObservableCollection again. The project seems dead, though, and I haven't used the recommend replacement yet (Obtics).
Try my ObservableComputations library:
agreement.Branches
.SelectingMany(x => x.Products)
.Distincting()
agreement.Branches
and Products
should be of type INotifyCollectionChanged
(ObservableCollection).
精彩评论