问题
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