Please help me convert this C# 2.0 snippet to Linq
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);
开发者_Python百科 }
// ~TODO
}
P.S. Check out my other, bounty question.
Look at the Union
operator:
return newSInstanceDictionary.Union(newSOperationDictionary).ToDictionary(x => x.Key, y => y.Value);
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
.
精彩评论