Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6183093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:18:31+00:00 2026-05-24T01:18:31+00:00

I have a collection of observables that generate state changes for a so-called Channel

  • 0

I have a collection of observables that generate state changes for a so-called Channel. And I have a ChannelSet that should monitor those channels.

I would like to write something like this: if one channel is operational, the channel set is up, else, the channel set is down.

IEnumerable<ChannelState> channelStates = ...;
if (channelStates.Any(cs => cs == ChannelState.Operational))
    channelSet.ChannelSetState = ChannelSetState.Up;
else
    channelSet.ChannelSetState = ChannelSetState.Down;

But where do I get my IEnumerable<ChannelState>? If I have 1 channel, I can simply subscribe to its state changes and modify the state of the channel set accordingly. For two channels, I could use CombineLatest:

Observable.CombineLatest(channel0States, channel1States, (cs0, cs1) =>
    {
        if (cs0 == ChannelSetState.Up || cs1 == ChannelSetState.Up)
            return ChannelSetState.Up;
        else
            return ChannelSetState.Down;
    });

But I have an IEnumerable<Channel> and a corresponding IEnumerable<IObservable<ChannelState>>. I’m looking for something like CombineLatest that is not limited to a fixed number of observables.

To complicate matters, the collection of channels can be added to and removed from. So once in a while, a channel will be added for example. The new channel also generates state changes that need to be incorporated.

So what I’m actually looking for is a function:

IEnumerable<IObservable<ChannelState>> --> IObservable<ChannelSetState>

that keeps up-to-date when the input changes. There should be some way to accomplish this using Rx but I can’t really figure out how.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:18:32+00:00Added an answer on May 24, 2026 at 1:18 am

    There’s a fairly straight forward way to do what you want with Rx, but you need to think in terms of observables only and not mix in enumerables.

    The function signature that you really need to think in terms of is:

    IObservable<IObservable<ChannelState>> --> IObservable<ChannelSetState>
    

    Here’s the function:

    Func<IObservable<IObservable<ChannelState>>, IObservable<ChannelSetState>> f =
        channelStates =>
            channelStates
                .Merge()
                .Select(cs => cs == ChannelState.Operational ? 1 : -1)
                .Scan(0, (cssn, csn) => cssn + csn)
                .Select(cssn => cssn > 0 ? ChannelSetState.Up : ChannelSetState.Down)
                .DistinctUntilChanged();
    

    It is important that each IObservable<ChannelState> in the IObservable<IObservable<ChannelState>> behaves properly for this to work.

    I’ve assumed that the ChannelState enum has an Idle state and that each IObservable<ChannelState> will produce zero or more pairs of Operational/Idle values (Operational followed by Idle) before completing.

    Also you said “the collection of channels can be added to and removed from” – thinking in terms of IEnumerable<IObservable<ChannelState>> this sounds reasonable – but in Rx you don’t have to worry about removes because each observable can signal its own completion. Once it signals completion then it is as if it has been removed from the collection because it can not produce any further values. So you only need to worry about adding to the collection – this is easy using subjects.

    So now the function can be used like so:

    var channelStatesSubject = new Subject<IObservable<ChannelState>>();
    var channelStates = channelStatesSubject.AsObservable();
    var channelSetStates = f(channelStates);
    
    channelSetStates.Subscribe(css => { /* ChannelSetState subscription code */ });
    
    channelStatesSubject.OnNext(/* IObservable<ChannelState> */);
    channelStatesSubject.OnNext(/* IObservable<ChannelState> */);
    channelStatesSubject.OnNext(/* IObservable<ChannelState> */);
    // etc
    

    I ran this using some test code, that used three random ChannelState observables, with a Do call in the f function for debugging, and got the following sequence:

    1
    Up
    2
    3
    2
    1
    2
    1
    0
    Down
    1
    Up
    0
    Down
    

    I think that’s what you’re after. Let me know if I’ve missed anything.


    As per the comments below, the ChannelState enum has multiple states, but only Operational means that the connection is up. So it’s very easy to add a DistinctUntilChanged operator to hide multiple “down” states. Here’s the code now:

    Func<IObservable<IObservable<ChannelState>>, IObservable<ChannelSetState>> f =
        channelStates =>
            channelStates
                .Merge()
                .Select(cs => cs == ChannelState.Operational ? 1 : -1)
                .DistinctUntilChanged()
                .Scan(0, (cssn, csn) => cssn + csn)
                .Select(cssn => cssn > 0 ? ChannelSetState.Up : ChannelSetState.Down)
                .DistinctUntilChanged();
    

    Added code to ensure that the first select query always starts with a 1. Here’s the code now:

    Func<IObservable<IObservable<ChannelState>>, IObservable<ChannelSetState>> f =
        channelStates =>
            channelStates
                .Merge()
                .Select(cs => cs == ChannelState.Operational ? 1 : -1)
                .StartWith(1)
                .DistinctUntilChanged()
                .Scan(0, (cssn, csn) => cssn + csn)
                .Select(cssn => cssn > 0 ? ChannelSetState.Up : ChannelSetState.Down)
                .DistinctUntilChanged();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Collection of items that I would like to display the Total
The situation is like this: I have an Observable Collection that has a bunch
I'm stumped here. I have an observable collection that holds business objects. I have
I have an observable collection of objects that I'd like to display on the
I have a TreeView that is bound to a collection class that I have
I have my class that has an internal observable collection. I want to pass
I have the following bit of code that modifies an observable collection of 'screens'
I have a combo box that I bind to an observable collection, which gets
I have an method that takes in an observable collection (returned from a webservice)
I have an extension class that is converting an Observable collection to list and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.