基本窗口操作 [英] Basic Window Operation
问题描述
我正在尝试使用签名方法:
http://msdn.microsoft.com/en-us/library/hh211621(v=vs.103).aspx
//测试对Window运算符的理解
/ /我想要开始/结束标记之间的值序列序列
//基因测试序列
var obs =(新列表< int>(){1,2,3,1,4 ,4,2,0,0,1,5,5,5,-1,2,0,0})。ToObservable();
//开始标记是值1
var begin =(来自b in obs
其中b == 1
选择b)
;
//结束标记是值2
var end =(来自e in obs
,其中e == 2
选择e)
;
//其他标记是其余的(不是开始或结束)
var other =(来自o $ obs
其中o!= 1&& o!= 2
选择o)
;
//这不能做我想要的,就是在开始/结束标记之间产生序列
var inner =((来自other.Window中的窗口)(开始,b => ; end.Take(1))
选择窗口
)
.Switch()
);
inner.Subscribe(i => {Console.WriteLine(" Sequence"); i.Dump();});
我希望以下
序列:
4
4
序列:
5
5
5
-1
但我得到:
obs observable cold 。 每次调用
订阅方法时(显式或隐式,例如,将observable作为参数传递给
Window 运算符),整个序列被推送到新的观察者。
结果,每个订阅都在观察序列中的不同位置。
例如,结束函数 end.Take(1) 看到
obs 中的第一个值,1 , 紧接着 2 然后停止。 它永远不会超过第二个元素。
要解决此问题,您可以发布 序列来共享订阅副作用。 这会将
cold 序列更改为 hot 序列。 例如:var obs = new [] {1,2,...,0}
.ToObservable()
.Publish ();
....
inner.Subscribe(...);
obs.Connect();请注意,您的订阅似乎也不正确。 它写出字符串"Sequence"。对于每个项目,因为您已向查询添加了
切换。 请注意,返回类型为 IObservable< int> 。 您可以通过删除
切换并简单地创建嵌套订阅来解决此问题,或者您可以删除
切换并更改窗口强>到缓冲区
返回 IObservable< IList< int>> 。
- Dave
I'm trying to use the method with signature: http://msdn.microsoft.com/en-us/library/hh211621(v=vs.103).aspx
// Test understanding of Window operator // I want the sequence of sequence of values which come between begin/end markers // Gen test sequence var obs = (new List<int>() { 1, 2, 3, 1, 4, 4, 2, 0, 0, 1, 5, 5, 5, -1, 2, 0, 0 }).ToObservable(); // begin markers are the value 1 var begin = (from b in obs where b == 1 select b) ; // end markers are the value 2 var end = (from e in obs where e == 2 select e) ; // other markers are all the rest (not begin or end) var other = (from o in obs where o != 1 && o != 2 select o) ; // This doesn't do what I want, which is to produce sequences between begin/end markers var inner = ((from window in other.Window(begin, b => end.Take(1)) select window ) .Switch() ); inner.Subscribe(i => { Console.WriteLine("Sequence"); i.Dump(); });
I would expect the following
Sequence:
4
4
Sequence:
5
5
5
-1
But I get:
Hi,
The obs observable is cold. Each time you call the Subscribe method (either explicitly or implicitly, e.g., by passing the observable as a parameter to the Window operator), the entire sequence is pushed to the new observer.
As a result, each of the subscriptions are observing a different position within the sequence.
For example, the closing function end.Take(1) sees the first value in obs, 1, followed immediately by 2 and then stops. It never goes past the second element.
To solve this problem, you can Publish the sequence to share subscription side-effects. This changes the cold sequence into a hot sequence. For example:
var obs = new[] { 1, 2, ..., 0 } .ToObservable() .Publish(); .... inner.Subscribe(...); obs.Connect();Note that your subscription doesn't appear to be correct either. It writes the string "Sequence" for every item, because you've added Switch to the query. Note the return type is IObservable<int>. You can fix this by removing Switch and simply creating a nested subscription, or you can remove Switch and change Window to Buffer to get back an IObservable<IList<int>>.
- Dave
这篇关于基本窗口操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!