select from Dictionary 找到值,但 select from ConcurrentDictionary 没有

发布于 2024-11-17 21:09:14 字数 1798 浏览 6 评论 0原文

今天我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城烟雨 2024-11-24 21:09:14

您正在调用 AddOrUpdate,第三个参数是它应该创建一个新的空列表,因此结果是您最终得到一个空列表。我猜测在这行代码之前的某个地方您正在添加具有相同密钥的条目。

另请注意,Equals 函数不正确。您正在比较参考值,而不是您在 GetHashCode 中使用的实际值。我建议您使用默认模板来覆盖 Equals:

     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;
        }
     }

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 the GetHashCode. I suggest you use the default template for overriding Equals:

     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;
        }
     }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文