我有以下Observable。
IObservable<MyDto> observable;
现在为了计算Observable中的项目数,我不能使用下面的代码,因为我的observable是Hot并且长时间运行,并且永远不会调用对'count'的订阅。
var count = observable.Count()
我希望每次物品到达时都能得到计数,这是我想要做的事情
observable.Subscribe(o => Console.WriteLine(" Object received "));
observable.Count().Subscribe(c => Console.WriteLine("Current count is " + c.ToString() + " but this is not final count, more are coming"));
我怎样才能做到这一点?
答案 0 :(得分:4)
使用Scan:
observable
.Scan(0, (count, _) => count + 1)
.Subscribe(count => Console.WriteLine("Current count is " + count));