Can this reactive code be improved? I have a view model that I'm migrating to RxUI. It exposes a collection of brokers along with a bool indicating whether there is a market (ie. at least one broker). In addition, it has a property for the currently selected broker (the user can choose a broker via a drop-down):

private IReactiveCollection&lt;SourceViewModel&gt; brokers;
public IReactiveCollection&lt;SourceViewModel&gt; Brokers
{
    get { return this.brokers; }
    private set { this.RaiseAndSetIfChanged(x =&gt; x.Brokers, ref this.brokers, value); }
}

private ObservableAsPropertyHelper&lt;bool&gt; isMarket;
public bool IsMarket
{
    get { return this.isMarket.Value; }
}

private SourceViewModel selectedBroker;
public SourceViewModel SelectedBroker
{
    get { return this.selectedBroker; }
    set { this.RaiseAndSetIfChanged(x =&gt; x.SelectedBroker, ref this.selectedBroker, value); }
}


I have these pieces hooked up and working, but I am not convinced I've done this in the most reactive-friendly fashion:

this.WhenAny(x =&gt; x.Volatilities, x =&gt; x.Value)
    .StartWith((ItemObservableCollection&lt;VolatilityViewModel&gt;)null)
    .Subscribe(x =&gt;
        {
            if (x == null)
            {
                // no volatilities, so definitely no market
                this.Brokers = null;
                this.SelectedBroker = null;
                this.isMarket = Observable.Return(false).ToProperty(this, y =&gt; y.IsMarket);
            }
            else
            {
                // we have volatilities, so potentially we have a market

                // TODO: is there a better means of doing this?
                this.Brokers = x.CreateDerivedCollection(y =&gt; y.Source);

                // TODO: is there a cleaner way to do this?
                this.Brokers.Changed
                    .Select(y =&gt; true)
                    .StartWith(true)
                    .Subscribe(y =&gt;
                        {
                            var volatility = x.FirstOrDefault();
                            this.SelectedBroker = volatility == null ? null : volatility.Source;
                        });

                // TODO: is there a cleaner way to do this?
                this.isMarket = this.Brokers.CollectionCountChanged.Select(y =&gt; y &gt; 0).StartWith(x.Count &gt; 0).ToProperty(this, y =&gt; y.IsMarket);
            }
        });


My Volatilities property is set when the user interacts with a specific set of vols. Everything hinges off of it. It's currently an ItemObservableCollection, but I may change it later to be a reactive collection. I don't believe that's relevant here, but I could be wrong.

The parts I'm concerned about have TODOs in the code, but to summarize:


when my Volatilities property changes, so too do the available brokers. I currently create a new derived collection each time Volatilities changes. I couldn't come up with any way to avoid this or implement it more cleanly.
when I recreate my brokers collection, I immediately set up a Changed observer so that any changes result in the first broker being selected. The reasoning is that the first broker will (once I do sorting) be the preferred broker, so any changes in the market should instigate the selection of the preferred broker. I couldn't think of a way to make this logic cleaner and simpler.
in addition, I also recreate my isMarket member, which is true whenever there is at least one broker in the brokers collection. Again, I'm not sure that this is the best approach to achieving this.


Can anyone weigh in with their thoughts on this?
I currently create a new derived collection each time Volatilities changes


This was a good idea, but in this case I'd just recreate the brokers list (since it's not a straight volatilities.Select(x =&gt; ...), it's a SelectMany. One thing that's important though, is whether you mutate (i.e. add and remove) items from Volatilities, or you only replace the list (i.e. via an API call, set this.Volatilities to a new collection). 

Pick one or the other, don't mix them or else your code will be complicated. Let's assume you're using ReactiveCollection and you're adding/removing to Volatilities.

Volatilities.CollectionCountChanged.StartWith(Volatilities.Count)
    .Select(_ =&gt; Volatilities.SelectMany(x =&gt; x.BrokerList))
    .ToProperty(this, x =&gt; x.Brokers);


Then, we can watch when someone changes Brokers - we'll do this in the constructor (along with everything else). Generally, if you're not calling RxUI methods in the constructor, you're Doing Something Wrong. You're wiring up on startup what will happen when various parts of your ViewModel change, those wires typically are constant:

this.WhenAny(x =&gt; x.Brokers, x =&gt; x.Value)
    .Subscribe(_ =&gt; SelectedBroker = Brokers.FirstOrDefault());


Now let's set up the IsMarket:

this.WhenAny(x =&gt; x.Brokers, x =&gt; x.Value)
    .Select(x =&gt; x.Count &gt; 0)
    .ToProperty(this, x =&gt; x.IsMarket);


What We Can Learn™

The key point here though, is that IsMarket is directly related to Brokers and nothing else - we should describe that relationship without mentioning Volatilities, then separately describe the relationship between Volatilities and Brokers. We don't want to mix the two.