使用 List 作为字典键 C#

本文关键字:字典 List customObject 使用 | 更新日期: 2023-09-27 18:32:01

我有一个字典,其键是一个列表,

如下所示
var dict = new Dictionary<List<MyKey>, Emp>(new MyCustomComparer());

我在实现列表比较器时遇到问题。即使该值存在,包含键也始终返回 false。这是我写的代码

程序.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();
    }
}

任何帮助将不胜感激。

问候

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

发布更改的代码

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