views:

56

answers:

2

This is not a homework ;) I need to both A) optimize the following code (between a TODO and a ~TODO) and B) convert it to [P]Linq. Better readability is desired. It might make sense to provide answers to A) and B) separately. Thanks!

lock (Status.LockObj)
{
    // TODO: find a better way to merge these dictionaries
    foreach (KeyValuePair<Guid, Message> sInstance in newSInstanceDictionary)
    {
        this.sInstanceDictionary.Add(sInstance.Key, sInstance.Value);
    }

    foreach (KeyValuePair<Guid, Message> sOperation in newSOperationDictionary)
    {
        this.sOperationDictionary.Add(sOperation.Key, sOperation.Value);
    }
    // ~TODO
}

P.S. Check out my other, bounty question.

+1  A: 

Look at the Union operator:

 return newSInstanceDictionary.Union(newSOperationDictionary).ToDictionary(x => x.Key, y => y.Value);
Oded
That won't return a dictionary.
SLaks
@SLaks - Added `ToDictionary` call.
Oded
+1  A: 

There are no LINQ methods that would help you here, although you could make your own extension methods.

If the dictionaries are empty before this code runs, you could call ToDictionary.

SLaks