这是System.Reactive的新手,但据我所知,我们可以订阅任何可观察到的集合,如果那里发生了什么事情,我会得到通知。
但是,当我在寻找一个框架来帮助我调度任务时,如果仅仅拥有一个已经填充了数据的可观察的集合,按时间过滤它们,并使它们在匹配某些条件时立即引发onnext事件,这将是巨大的。
假设我们有一个类
Public Class Appointment
Property Notification As DateTime
End Class然后我们有一个List<IObservable>,我们订阅它,然后我们指定类似于where的东西,但不是在添加新的东西时,而是在匹配的时候。在本例中,当前的datetime Now()和任何约会
src.WhenWhere(x => x.Notification < Now())或者应该使用自定义可观察对象来完成此操作?
发布于 2013-01-03 05:34:23
你可以这样做..。请注意,您必须在约会上放置一些标志来表明通知已发送,并将其添加到where子句中,否则,一旦约会时间为< DateTime.Now,它将一遍又一遍地发送结果。
void Main() {
var appointments = new List<Appointment> {
new Appointment { Id = 1, Notification = DateTime.Now.AddMilliseconds(4000) },
new Appointment { Id = 2, Notification = DateTime.Now.AddMilliseconds(7000) }
};
var q = from t in Observable.Generate(DateTime.Now, _ => true, _ => _, _ => DateTime.Now, _ => TimeSpan.FromSeconds(1))
from a in appointments
where a.Notification < t
select new { a.Id, a.Notification };
q.Dump();}
public class Appointment {
public int Id { get; set; }
public DateTime Notification { get; set; }
}https://stackoverflow.com/questions/14128406
复制相似问题