Keeping an observable collection up to date 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&lt;MarketOrder&gt; oldList, 
                          List&lt;MarketOrder&gt; newList)
    {
        int oi = 0; // old list iterator
        int ni = 0; // new list iterator

        while (ni &lt; 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 &lt; newList[ni].Price)
            {
                oldList.RemoveAt(oi);
            }
            else if (oldList[oi].Price &gt; newList[ni].Price)
            {
                oldList.Insert(oi, newList[ni]);
            }
        }

        while (oldList.Count &gt; 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; }
}
After studying your code snippet for a bit here's what I came up with. 

I wouldn't call them iterators. They're really acting more as indexing operators that doubles as a loop counter. Furthermore, they're always incremented in sync and they have the exact same initial value.

int oi = 0; // old list iterator
int ni = 0; // new list iterator


So I would replace with simply:

int i = 0;


The second loop for trimming the extra elements:

while (oldList.Count &gt; ni)
{
  oldList.RemoveAt(ni);
}


The post-condition for i after the first loop has to be equal to newList.Count otherwise we would still be looping the first while. Thusly, changing the condition to this is clearer:

while (oldList.Count &gt; newList.Count)




After tracing your UpdateMarketOrderList method, the discernible end effect, from what I can tell, just seems to be a replacement of the oldList with the newList. If that's really the desired end consequence of running this function, why not simplify it, succinctly to this instead?

public static void UpdateMarketOrderList( ObservableCollection&lt;MarketOrder&gt; oldList, 
                                          List&lt;MarketOrder&gt; newList )
{
    oldList.Clear();

    for(int i = 0; i &lt; newList.Count; ++i)
    {
        oldList.Add(newList[i]);
    }
}


Then again you didn't specify in your question what the pre/post-conditions are for UpdateMarketOrderList, which is something that does matter.



After reading OP's comment that fired change events are important, I would refactor the main loop like this:

    while (i &lt; newList.Count)
    {
        if (i == oldList.Count ||
            oldList[i].Price &gt; newList[i].Price)
        {
            oldList.Insert(i, newList[ni]);    
        }
        else if (oldList[i].Price &lt; newList[i].Price)
        {
            oldList.RemoveAt(i);
            continue;
        }
        else if (oldList[i].Quantity != newList[i].Quantity) 
              // &amp;&amp; oldList[i].Price == newList[i].Price
        {
            Debug.Assert(oldList[i].Price == newList[i].Price);
            oldList[i].Quantity = newList[i].Quantity;
        }

        ++i;
    }


With this refactor you decrease the indent level and tighten the loop a bit while preserving existing behavior. The key thing to realize is that RemoveAt doesn't increment the loop counter. Your previous code did that in a slightly less direct manner.

My gut feeling is that this could be refactored further still but without a good idea of the expected side-effects and other requirements for this method, it will be difficult to refactor this safely.