C# 从字典中选择查找值,但从ConcurrentDictionary中选择不查找值
今天我用ConcurrentDictionary和Dictionary做了一些测试:C# 从字典中选择查找值,但从ConcurrentDictionary中选择不查找值,c#,c#-4.0,dictionary,parallel-processing,concurrentdictionary,C#,C# 4.0,Dictionary,Parallel Processing,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) {
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();
ConcurrentDictionary\u test=new ConcurrentDictionary();
字典_test2=新字典();
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")
}
);
\u测试添加或更新(第10单元,
新名单
{
新的MyTest(1,1,“ovpSOMEVALUEValue”),
新的MyTest(1,2,“ocpSOMEVALUEValue”)
},
(键,值)=>value=newlist());
_test2.Add(单元格10,
新名单
{
新的MyTest(1,1,“ovpSOMEVALUEValue”),
新的MyTest(1,2,“ocpSOMEVALUEValue”)
}
);
您正在调用AddOrUpdate
,第三个参数是它应该创建一个新的空列表,因此结果是您得到一个空列表。我猜在这行代码之前的某个地方,您正在添加一个具有相同键的条目
还要注意,Equals
函数不正确。您是在参考上进行比较,而不是在GetHashCode
中使用的实际值上进行比较。我建议您使用默认模板覆盖等于:
protected bool Equals(x other) {
return Row == other.Row && Col == other.Col && string.Equals(Value, other.Value);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((x) obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = Row;
hashCode = (hashCode*397) ^ Col;
hashCode = (hashCode*397) ^ (Value != null ? Value.GetHashCode() : 0);
return hashCode;
}
}
您正在调用
AddOrUpdate
,您的第三个参数是它应该创建一个新的空列表,因此结果是您得到一个空列表。我猜在这行代码之前的某个地方,您正在添加一个具有相同键的条目
还要注意,Equals
函数不正确。您是在参考上进行比较,而不是在GetHashCode
中使用的实际值上进行比较。我建议您使用默认模板覆盖等于:
protected bool Equals(x other) {
return Row == other.Row && Col == other.Col && string.Equals(Value, other.Value);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((x) obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = Row;
hashCode = (hashCode*397) ^ Col;
hashCode = (hashCode*397) ^ (Value != null ? Value.GetHashCode() : 0);
return hashCode;
}
}
请发布用于向字典添加值的代码。_test.AddOrUpdate(cell10,new List{new MyTest(1,1,“ovpSOMEVALUEValue”),new MyTest(1,2,“ocpSOMEVALUEValue”)},(key,value)=>value=new List();对于常规字典:_test2.Add(cell10,newlist{newmytest(1,1,“ovpSOMEVALUEValue”)、newmytest(1,2,“ocpSOMEVALUEValue”);请发布用于向字典添加值的代码。_test.AddOrUpdate(cell10,new List{new MyTest(1,1,“ovpSOMEVALUEValue”),new MyTest(1,2,“ocpSOMEVALUEValue”)},(key,value)=>value=new List();对于常规字典:_test2.Add(cell10,newlist{newmytest(1,1,“ovpSOMEVALUEValue”)、newmytest(1,2,“ocpSOMEVALUEValue”);