开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜