Search code examples
c#.netreactive-programmingobservablecollection

ObservableCollection showing items in incorrect order after Clear


I have a ObservableCollection where I add items and occasionally Clear. Then there is reactive pipeline where the idea is to have current contents of the ObservableCollection in insertion order.

Before doing the first Clear, everything works. The items are in correct order. However after doing Clear, order at the pipeline is not correct. Sometimes added items end up as the first, sometimes as the last.

var collection = new ObservableCollection<Item> = new(new List<Item>());

collection
  .ToObservableChangeSet(x => x)
  .ToCollection()
  .Do(coll =>
      // I want to have correct order of the items here.
            {
                Console.WriteLine($"---");
                foreach (var x in coll)
                {
                    Console.WriteLine($"x={x.Timestamp}");
                }
            })
  })
  ...
  .Subscribe(...)

// do some Adds and Clears to collection


Solution

  • After doing this change it seems to work correctly:

    .ToObservableChangeSet(x => x)
    

    to

    .ToObservableChangeSet()