c# - 你如何用响应式扩展扇出 observables

标签 c# system.reactive

我有一个服务,我想把它变成一个 rx observable

服务有一个接口(interface)

    IEnumerable<Price> FetchUpdatedPrices()
    {
       //do work to return changed data since last update
    }

我的想法是使用 rx 来允许消费者订阅更新。该实现将每隔 x 秒轮询一次服务并调用观察者。

我想到了以下内容

    public IDisposable Subscribe(IObserver<IEnumerable<Price>> observer)
    {

        IObservable<IEnumerable<Price>> updatedPrices = Observable.Interval(new TimeSpan(0, 0, 1))
            .Select(r => FetchUpdatedPrices());

        return updatedPrices.Subscribe(observer);
    }

问题是我希望观察者看到 IObservable<Price>而不是 IObservable<IEnumerable<Price>>

谁能给这个 Rx 菜鸟任何关于如何做到这一点的建议?

最佳答案

SelectMany怎么样? ?

IObservable<IEnumerable<Price>> updatedPrices = Observable.Interval(new TimeSpan(0, 0, 1))
    .SelectMany(r => FetchUpdatedPrices());

关于c# - 你如何用响应式扩展扇出 observables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9093499/

相关文章:

c# - 在图像上绘制缩放图片

c# - 为什么 List.Sort() 是实例方法而 Array.Sort() 是静态的?

c# - ArgumentException:C# 中的 'Illegal characters in path'

c# - 为什么编译器在从方法返回字符串时会创建一条似乎什么都不做的指令?

c# - 通过控制台界面访问编译器时包含库

c# - 为什么 Observable.ToEnumerable() 在底层序列完成之前不会产生值?

c# - 一个热可观察对象,根据系统状态切换它发出的内容(带有代码示例)

java - 没有得到 BehaviorSubject

c# - 使用 System.Reactive 观察 ObservableCollection 中项目的 PropertyChanged

c# - 热可观察和 IDisposable