title: Rx how to group by a key a complex object and later do SelectMany without "stopping" the stream?
body: <p>This is related to my other question <a href="https://stackoverflow.com/questions/19313360/how-to-partition-groupby-a-stream-and-monitor-absence-of-elements-in-rx-within">here</a>. <a href="https://stackoverflow.com/users/87427/james-world">James World</a> presented a solution as follows:</p>
  
  <pre><code>// idStream is an IObservable&lt;int&gt; of the input stream of IDs
  // alarmInterval is a Func&lt;int, TimeSpan&gt; that gets the interval given the ID
  var idAlarmStream = idStream
  .GroupByUntil(key =&gt; key, grp =&gt; grp.Throttle(alarmInterval(grp.Key)))
  .SelectMany(grp =&gt; grp.IgnoreElements().Concat(Observable.Return(grp.Key)));
  </code></pre>
  
  <p><strong>&lt;edit 2:</strong></p>
  
  <p><strong>Question:</strong> How do I start the timers immediately without waiting for the first events to arrive? That's the root problem in my question, I guess. For that end, I planned on sending off dummy objects with the IDs I know should be there. But as I write in following, I ended up with some other problems. Nevertheless, I'd think solving that too would be interesting.</p>
  
  <p>Forwards with the other interesting parts then! Now, if I'd like to group a complex object like the following and group by the key as follows (won't compile)</p>
  
  <pre><code>var idAlarmStream = idStream
      .Select(i =&gt; new { Id = i, IsTest = true })
      .GroupByUntil(key =&gt; key.Id, grp =&gt; grp.Throttle(alarmInterval(grp.Key)))
      .SelectMany(grp =&gt; grp.IgnoreElements().Concat(Observable.Return(grp.Key)));
  </code></pre>
  
  <p>then I get into trouble. I'm unable to modify the part about <code>SelectMany</code>, <code>Concat</code> and <code>Observable.Return</code> so that the query would work as before. For instance, if I make query as</p>
  
  <pre><code>var idAlarmStream = idStream
      .Select(i =&gt; new { Id = i, IsTest = true })
      .GroupByUntil(key =&gt; key.Id, grp =&gt; grp.Throttle(alarmInterval(grp.Key)))
      .SelectMany(grp =&gt; grp.IgnoreElements().Concat(Observable.Return(grp.Key.First())))
      .Subscribe(i =&gt; Console.WriteLine(i.Id + "-" + i.IsTest);
  </code></pre>
  
  <p>Then two events are needed before an output can be observed in the <code>Subscribe</code>. It's the effect of the call to <code>First</code>, I gather. Furthermore, I woul like to use the complex object attributes in the call to <code>alarmInterval</code> too.</p>
  
  <p>Can someone offer an explanation what's going on, perhaps even a solution? The problem in going with unmodified solution is that the grouping doesn't look Ids alone for the key value, but also the IsTest field.</p>
  
  <p><strong>&lt;edit:</strong> As a note, the problem probably could be solved firsly by creating an explicit class or struct and then that implements a custom <code>IEquatable</code> and secondly then using James' code as-is so that grouping would happen by IDs alone. It feels like hack though.</p>