Aid*_*dan 3 c# events system.reactive
我有一个由大量事件填充的可观察集合。我想从 EventArg 中获取信息,按名称对其进行分组,然后为每个名称选择最大日期。我试过这个:
_subscription = Observable
.FromEventPattern<NewLoanEventHandler, NewLoanEventArgs>(
h => loan.NewLoanEvent += h,
h => loan.NewLoanEvent -= h)
.Select(a => a.EventArgs.Counterpatry)
.GroupBy(c => c.Name)
.SelectMany(grp => grp.Max( c => c.MaturityDate ).Select( maturity => new {grp.Key, maturity}) )
.Subscribe(
i => Console.WriteLine("{0} --> {1}", i.Key, i.maturity),
Console.WriteLine,
() => Console.WriteLine("completed")
);
Run Code Online (Sandbox Code Playgroud)
我认为它可能会做我想做的事,但订阅永远不会完成:我永远不会收到完整的消息,也没有得到任何输出。也就是说,我怀疑,因为 Observable 仍在等待更多事件。我如何告诉它停止等待并给我我的输出?
如果您只等待来自服务的单个响应,请考虑在查询表达式中使用 .Take(1) 或 .FirstAsync() :
_subscription = Observable
.FromEventPattern<NewLoanEventHandler, NewLoanEventArgs>(
h => loan.NewLoanEvent += h,
h => loan.NewLoanEvent -= h)
.Take(1)
.Select(a => a.EventArgs.Counterpatry)
.GroupBy(c => c.Name)
.SelectMany(grp => grp.Max( c => c.MaturityDate ).Select( maturity => new {grp.Key, maturity}) )
.Subscribe(
i => Console.WriteLine("{0} --> {1}", i.Key, i.maturity),
Console.WriteLine,
() => Console.WriteLine("completed")
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
197 次 |
| 最近记录: |