使用List <customobject>作为字典键c#</customobject>

时间:2014-11-26 16:46:39

标签: c# c#-4.0

我有一个字典,其中的键是一个列表,如下所示

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

我在实现List比较器时遇到问题。即使该值存在,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
{
}

Comparer class

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

}

非常感谢任何帮助。

问候

2 个答案:

答案 0 :(得分:0)

发布更改后的代码

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

comparer class

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

}

答案 1 :(得分:0)

另一种方法:

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