SelectMany and Concat reactive extensions

I understand that the behavior of SelectMany is to efficiently combine the results of each value created into a single stream, so the ordering is non-deterministic.

How do I do something similar to concatAll in RxJs in C #.

var obs = Observable.Range (1, 10).SelectMany (x => {
return Observable.Interval (TimeSpan.FromSeconds(10 - x)).Take (3);
}).Concat();

      

This is effectively what I want to do, given the range, Wait a bit for each and then concat in the order they started. Obviously, this is an example of a toy, but the idea is there.

Blair

+3


source to share


1 answer


Use Select

instead SelectMany

. The overload Concat

you want to use works for IObservable<IObservable<T>>

, so just project the inner sequences, don't flatten them.

var obs = Observable.Range(1, 10)
                    .Select(x => Observable.Interval(TimeSpan.FromSeconds(10 - x)).Take(3))
                    .Concat();

      



Note that each subcriterion is Interval

deferred by Concat

; that is, the first one Interval

starts immediately after signing, but all the remaining intervals are generated and placed in the unsubscribed queue. It doesn't like it Concat

will subscribe to everything and then repeat the values ​​in the correct order later.

+3


source







All Articles