我試圖找到一種更新ConcurrentDictionary中的值的優雅方式。我創建的什麼,我想下面來實現一個簡單的例子:LINQ從另一個列表更新一個列表
ConcurrentDictionary<int, MyDataClass> dataLookup = new ConcurrentDictionary<int, MyDataClass>();
// Initialise the example dataLookup with some dummy data
new List<MyDataClass>
{
new MyDataClass { Id = 1, ValueProperty = 0 },
new MyDataClass { Id = 2, ValueProperty = 0 },
new MyDataClass { Id = 3, ValueProperty = 0 },
new MyDataClass { Id = 4, ValueProperty = 0 },
new MyDataClass { Id = 5, ValueProperty = 0 }
}.ForEach(myClass => dataLookup.TryAdd (myClass.Id, myClass));
// incoming results that need to be fed into the above dataLookup
List<MyDataClass> newDataReceived = new List<MyDataClass>
{
new MyDataClass { Id = 1, ValueProperty = 111 },
new MyDataClass { Id = 3, ValueProperty = 222 },
new MyDataClass { Id = 5, ValueProperty = 333 }
};
所以在上面的例子中,我想設置ValueProperty在dataLookup ConcurrentDictionary與1的內徑,3 & 5至111 ,222和333。我可以將newDataReceived對象更改爲我想要的任何東西,但我幾乎堅持將dataLookup作爲ConcurrentDictionary。
目前我正在遍歷列表,但我正在尋找一些關於使用LINQ來提高此任務的建議。