I'm using an ObservableCollection in a WPF app. The source of the data doesn't provide changes. It provides snapshots instead. The method I wrote to update the ObservableCollection works but it seems inelegant. Is there a better way to do this?
public static void UpdateMarketOrderList(
ObservableCollection<MarketOrder> oldList,
List<MarketOrder> newList)
{
int oi = 0; // old list iterator
int ni = 0; // new list iterator
while (ni < newList.Count)
{
if (oi == oldList.Count)
{
oldList.Insert(oi, newList[ni]);
}
else if (oldList[oi].Price == newList[ni].Price)
{
if (oldList[oi].Quantity != newList[ni].Quantity)
{
oldList[oi].Quantity = newList[ni].Quantity;
}
++oi;
++ni;
}
else if (oldList[oi].Price < newList[ni].Price)
{
oldList.RemoveAt(oi);
}
else if (oldList[oi].Price > newList[ni].Price)
{
oldList.Insert(oi, newList[ni]);
}
}
while (oldList.Count > ni)
{
oldList.RemoveAt(ni);
}
}
If it matters, the definition MarketOrder can be considered to be this:
public class MarketOrder
{
public decimal Price { get; set; }
public decimal Quantity { get; set; }
}
Price
and there are no duplicates per Price? \$\endgroup\$