谁能告诉我是什么导致以下代码中出现这种看似奇怪的 Do 行为?我希望每个 OnNext 调用一次 Do 处理程序。
using System;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using NUnit.Framework;
[TestFixture]
public class WhyDoesDoActSoWierd
{
[Test]
public void ButWhy()
{
var doCount = 0;
var observable = new Subject<int>();
var stream = observable.Do( x => doCount++ );
var subs =
(from x in stream
where x % 2 == 1
from y in stream
where y % 2 == 0
&& y == x + 1
select new { x, y })
.Subscribe( x => Console.WriteLine( "{0}, {1}", x.x, x.y ) );
observable.OnNext( 1 );
// doCount == 1
observable.OnNext( 2 );
// doCount == 3
observable.OnNext( 3 );
// doCount == 5
observable.OnNext( 4 );
// doCount == 8
observable.OnNext( 5 );
// doCount == 11
observable.OnNext( 6 );
// doCount == 15
Assert.AreEqual( 6, doCount );
}
}