反应式扩展 SelectMany 和 Concat [英] Reactive Extensions SelectMany and Concat

查看:29
本文介绍了反应式扩展 SelectMany 和 Concat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解 SelectMany 的行为是有效地将产生的每个值的结果合并到一个流中,因此排序是不确定的.

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.

如何在 C# 中的 RxJs 中执行类似于 concatAll 的操作.

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.

布莱尔

推荐答案

使用 Select,而不是 SelectMany.您要使用的 Concat 重载适用于 IObservable>,因此只需投影内部序列,不要将它们展平.

Use Select, not SelectMany. The Concat overload that you want to use works on an IObservable<IObservable<T>>, so simply 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();

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

Note that the subscrition of each Interval is deferred by using Concat; i.e., the first Interval starts right away when you subscribe, but all of the remaining intervals are generated and enqueued without subscription. It's not like Concat will subscribe to everything and then replay the values in the correct order later.

这篇关于反应式扩展 SelectMany 和 Concat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆