StartWith
will fix your problem. Also, I'm not sure that you want ItemChanged, that signals when an item in the list changes, not when an item is Added or Removed. Here, you probably also want to listen to Changed
.
While it's tempting to try to keep a "running total" in this way, it's actually Pretty Hard to do this correctly because of Resets and Moves. An easier approach would be to abuse the fact that CPUs are really fast and just recompute it on every change:
_collection.ChangeTrackingEnabled = true;
Observable.Merge(
_collection.Changed.Select(_ => Unit.Default),
_collection.ItemChanged.Select(_ => Unit.Default))
.Select(_ => _collection.Sum(x => x.NumberToSum));
Another approach that isn't very good for aggregates like this but can also be super useful, is ActOnEveryObject
, which will execute a block on every item in a list, and correctly handles initialization, resets, and moves.