2013-07-29 45 views
1

我有一個可觀察的集合,它是從一系列事件中填充的。我想從EventArg獲取信息,按名稱對其進行分組,然後選擇每個名稱的最大日期。我曾經嘗試這樣做:FromEventPattern如何知道事件何時完成?

_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") 
       ); 

我想可能做我想做的,但認購永遠不會完成:我從來沒有得到完整的信息,我沒有得到任何輸出。也就是說,我懷疑,因爲Observable仍在等待更多事件。我如何告訴它停止等待並給我輸出?

回答

4

如果你只等待您服務的單一響應,可以考慮使用。取(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") 
       ); 
+0

您也可以使用'TakeUntil'或'GroupByUntil 'combinators。 –