0
今天,我在做一些測試與ConcurrentDictionary和詞典選擇:選擇從字典查找值,但來自ConcurrentDictionary不
class MyTest
{
public int Row { get; private set; }
public int Col { get; private set; }
public string Value { get; private set; }
public MyTest(int row, int col, string value)
{
this.Col = col;
this.Row = row;
this.Value = value;
}
public override bool Equals(object obj)
{
MyTest other = obj as MyTest;
return base.Equals(other);
}
public override int GetHashCode()
{
return (Col.GetHashCode()^Row.GetHashCode()^Value.GetHashCode());
}
}
使用實體上面,我創建並填補了ConcurrentDictionary和解釋,並試圖代碼如下:
ConcurrentDictionary<MyTest, List<MyTest>> _test = new ConcurrentDictionary<MyTest, List<MyTest>>();
Dictionary<MyTest, List<MyTest>> _test2 = new Dictionary<MyTest, List<MyTest>>();
MyTest dunno = _test.Values.AsParallel().Select(x => x.Find(a => a.Col == 1 && a.Row == 1)).FirstOrDefault();
MyTest dunno2 = _test2.Values.AsParallel().Select(x => x.Find(a => a.Col == 1 && a.Row == 1)).FirstOrDefault();
第一個返回的值,但第二個不是,我做錯了什麼?
這是用於添加值的代碼:要調用AddOrUpdate
_test.AddOrUpdate(cell10,
new List<MyTest>
{
new MyTest(1, 1, "ovpSOMEVALUEValue"),
new MyTest(1, 2, "ocpSOMEVALUEValue")
},
(key, value) => value = new List<MyTest>());
_test2.Add(cell10,
new List<MyTest>
{
new MyTest(1, 1, "ovpSOMEVALUEValue"),
new MyTest(1, 2, "ocpSOMEVALUEValue")
}
);
請發佈用於向字典中添加值的代碼。 –
_test.AddOrUpdate(cell10,new List {new MyTest(1,1,「ovpSOMEVALUEValue」),new MyTest(1,2,「ocpSOMEVALUEValue」)},(key,value)=> value = new列表() );對於常規字典:_test2.Add(cell10,new List {new MyTest(1,1,「ovpSOMEVALUEValue」),new MyTest(1,2,「ocpSOMEVALUEValue」)}); –