繁体   English   中英

使用清单 <customObject> 作为字典键C#

[英]using List<customObject> as dictionary key c#

我有一本字典,其关键字是一个清单,如下所示

var dict = new Dictionary<List<MyKey>, Emp>(new MyCustomComparer());

我在实现列表比较器时遇到了麻烦。 即使该值存在,containskey始终返回false。 这是我写的代码

Program.cs

        var dict = new Dictionary<List<MyKey>, Emp>(new MyCustomComparer());

        var key1 = new List<MyKey>
                       {
                           {new MyKey{ Name = "string1"}}
                       };

        dict.Add(key1, new Emp());
        var key2 = new List<MyKey>
                       {
                           {new MyKey{ Name = "string1"}}
                       };


        if (!dict.ContainsKey(key2))
        {
            dict.Add(key2, new Emp());
        }

重点班

public class MyKey
{
    public string Name { get; set; }

}

public class Emp
{
}

比较器类

public class MyCustomComparer : IEqualityComparer<List<MyKey>>
{
    public bool Equals(List<MyKey> x, List<MyKey> y)
    {
        return x.SequenceEqual(y);
    }

    public int GetHashCode(List<MyKey> obj)
    {
        return string.Join(",", obj.Select(s => s.Name)).GetHashCode();
    }

}

任何帮助都感激不尽。

问候

发布更改的代码

    var dict = new Dictionary<List<MyKey>, Emp>(new MyCustomComparer());

    var key1 = new List<MyKey>
                   {
                       {new MyKey{ Name = "string1"}}
                   };

    dict.Add(key1, new Emp());
    var key2 = new List<MyKey>
                   {
                       {new MyKey{ Name = "string1"}}
                   };


    if (!dict.ContainsKey(key2))
    {
        dict.Add(key2, new Emp());
    }


public class MyKey
{
    public string Name { get; set; }

    public override int GetHashCode()
    {
        return Name.GetHashCode();
    }

    public override bool Equals(object obj)
    {
        var myKey = obj as MyKey;
        if (myKey != null)
        {
            return Name == myKey.Name;
        }
        return false;
    }
}

public class Emp
{
}

比较器类

public class MyCustomComparer : IEqualityComparer<IEnumerable<MyKey>>
{
    public bool Equals(IEnumerable<MyKey> x, IEnumerable<MyKey> y)
    {
        return x.SequenceEqual(y);
    }

    public int GetHashCode(IEnumerable<MyKey> obj)
    {
        return string.Join(",", obj.Select(s => s.Name)).GetHashCode();
    }

}

另一种方法:

    public class MyCustomKey : ReadOnlyCollection<MyKey>
    {
        public override int GetHashCode()
        {
            return this.Aggregate(0, (c, i) => c + i.Name.GetHashCode()*i.Name.Length);
        }

        public override bool Equals(object obj)
        {
            var myKey = obj as MyCustomKey;
            if (myKey!= null && myKey.Count == this.Count)
            {
                return myKey.Zip(this, (i, j) => i.Name == j.Name).All(i=>i);
            }
            return false;
        }
    }

    var dict = new Dictionary<MyCustomKey, Emp>();

    var key1 = new MyCustomKey(new[] {new MyKey {Name = "string1"}});
    dict.Add(key1, new Emp());
    var key2 = new MyCustomKey(new[] {new MyKey {Name = "string1"}});

    if (!dict.ContainsKey(key2))
    {
         dict.Add(key2, new Emp());
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM