6

I understand that the behaviour of SelectMany is to effectively merge the results of each value produced into a single stream so the ordering in nondeterministic.

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 a Range, Wait a bit for each then concat in the order that they started in. Obviously this is a toy example but the idea is there.

Blair

4

1 回答 1

6

使用Select,不使用SelectManyConcat您要使用的重载适用于IObservable<IObservable<T>>,因此只需投影内部序列,不要将它们展平。

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

请注意,每个的订阅都Interval通过使用Concat;延迟。即,第一个Interval在您订阅时立即开始,但所有剩余的间隔都是在没有订阅的情况下生成和排队的。这不像Concat会订阅所有内容,然后稍后以正确的顺序重播这些值。

于 2014-10-10T13:59:14.343 回答