Rx how to take first n elements of a sequence within time interval and ignore others

╄→尐↘猪︶ㄣ 提交于 2020-01-15 13:41:58

问题


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


回答1:


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));


来源:https://stackoverflow.com/questions/31125466/rx-how-to-take-first-n-elements-of-a-sequence-within-time-interval-and-ignore-ot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!