Rx如何在时间间隔内取序列的前n个元素并忽略其他元素 [英] Rx how to take first n elements of a sequence within time interval and ignore others

查看:26
本文介绍了Rx如何在时间间隔内取序列的前n个元素并忽略其他元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的程序中使用 Rx 并希望为 observable 创建订阅,该订阅在一分钟的时间间隔内需要 5 个第一个元素并忽略其他元素.例如,

I'm using Rx in my programm and want to create subscription for observable that takes 5 first elements within one minute time interval and ignores others. For example,

Sequence: -1---2--3--4-5---6---7-8--------------
Interval: |------------------|------------------|
Result:   |1---2--3--4-5-----|-7-8--------------|

有什么想法吗?提前致谢

Any thoughts? Thanks in advance

推荐答案

Window + SelectMany + Take 在这种情况下可以工作:

Window + SelectMany + Take would work in this case:

var subscription = source.Window(TimeSpan.FromMinutes(1))
      .SelectMany(w => w.Take(5))
      .Subscribe(item => Console.WriteLine(item));

这篇关于Rx如何在时间间隔内取序列的前n个元素并忽略其他元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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