select from Dictionary 找到值,但 select from ConcurrentDictionary 没有
今天我正在使用 ConcurrentDictionary 和 Dictionary 进行一些测试:
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 和 Dictionary 并尝试了下面的代码:
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();
第一个返回值,但第二个不返回,我做错了什么?
这是用于添加值的代码:
_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")
}
);
Today i was doing some tests with the ConcurrentDictionary and Dictionary:
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());
}
}
Using the Entity above i created and filled a ConcurrentDictionary and a Dictionary and tried the code below:
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();
The first one returns the value but the second one not, what am i doing wrong?
This is the code used to add the values:
_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")
}
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在调用 AddOrUpdate,第三个参数是它应该创建一个新的空列表,因此结果是您最终得到一个空列表。我猜测在这行代码之前的某个地方您正在添加具有相同密钥的条目。
另请注意,
Equals
函数不正确。您正在比较参考值,而不是您在GetHashCode
中使用的实际值。我建议您使用默认模板来覆盖 Equals:You are calling
AddOrUpdate
, and your third parameter is that it should create a new empty list, thus the result is that you end up with an empty list. I am guessing that somewhere before this line of code you are adding an entry with the same key.Also notice that the
Equals
function is incorrect. You are comparing on reference, not on the actual values you use in theGetHashCode
. I suggest you use the default template for overriding Equals: