Could you try something like the following:
foreach (var tree in newTrees)
{
var keyValuePair = _myObject.Trees.Where(s => s.Key == tree.Key).First();
keyValuePair = new KeyValuePair<Tree, int>(tree.Key, tree.Value);
}
Now you are looping only the newTrees
and the Where(...)
finds the corresponding element(s) in _myObject.Trees
without explicitly looping. Note that the call to First()
assumes that any key in newTrees
already exists in _myObject.Trees
and that it exists only once.
I hope it helps!