繁体   English   中英

根据字典比较两个对象的相等性

[英]Compare equality of two objects based on dictionaries

我有两个具有以下定义的对象:

public static Dictionary<string, Container> cont1 = new Dictionary<string, Container>();
public static Dictionary<string, Container> cont2 = new Dictionary<string, Container>();

Container类的架构如下:

public class Container
{
    public string IDx { get; set; }
    public string IDy { get; set; }
    public string Name { get; set; }
    public Dictionary<string, Sub> Subs = new Dictionary<string, Sub>();
}

public class Sub
{
    public string Namex { get; set; }
    public string Namey { get; set; }
    public string Value { get; set; }
    public Dictionary<string, string> Paths { get; set; }
}

我的问题是:如何深入检查 cont1和cont2的公平性? 我的意思是每个成员的平等和价值甚至在Subs对象中都深处。

在这种情况下,c#中是否有任何功能,或者我必须根据自己的对象结构编写一个自定义方法来检查相等性;

第二个问题:如果我可以创建两个不同的产品副本,则可以消除均等问题。 我的意思是说我们有一个包含所有成员和值的基本Container对象,然后创建Container的两个单独副本 ,即cont1和cont2,这两个副本更改cont1中的值不会更改cont2中的相同值。

注意1 :此克隆方法不起作用:

cont2 = new Dictionary<string, Container>(cont1);

注意2 :在其他答案中,大多数建议的方法都是基于一个一级字典(使用循环或LINQ进行检查),而不是当对象中具有属性和字典对象(具有自己的属性)时。

字典是一个序列,因此通常您可能要寻找的是Enumerable<T>.SequenceEquals ,它允许传递IEquityComparer<T>

您的序列(字典)是IEnumerable<KeyValuePair<string,Container>>因此您需要一个比较器,该比较器实现IEquityComparer<IEnumerable<KeyValuePair<string,Container>>> (那是很多花括号!)。

var equal = cont1.SequenceEquals(cont2, new StringContainerPairEquityComparer());

请注意,不能保证元素字典的顺序,因此要正确使用该方法,您可能应该在比较序列之前使用OrderBy但这会增加该方法的效率。


对于第二个问题,您要尝试的是克隆词典。 通常,您的Container应实现ICloneable接口,然后可以使用该接口创建副本

var cont2 = cont1.ToDictionary(k => k.Key, v => v.Value.Clone());

是的,您必须编写一个自定义方法来根据自己的对象结构检查是否相等。 我将提供一个自定义的IEqualityComparer<Container>和一个IEqualityComparer<Sub>如下所示(基于此的 GetHashCode实现):

public class ContainerCheck : IEqualityComparer<Container>
{
    private SubCheck subChecker = new SubCheck();
    public bool Equals(Container x, Container y)
    {
        if (ReferenceEquals(x, y))
            return true;
        if (x == null || y == null)
            return false;
        if (x.IDx != y.IDx || x.IDy != y.IDy || x.Name != y.Name)
            return false;
        // check dictionary
        if (ReferenceEquals(x.Subs, y.Subs))
            return true;
        if (x.Subs == null || y.Subs == null || x.Subs.Count != y.Subs.Count)
            return false;
        foreach (var kv in x.Subs)
            if (!y.Subs.ContainsKey(kv.Key) || subChecker.Equals(y.Subs[kv.Key], kv.Value))
                return false;
        return true;

    }

    public int GetHashCode(Container obj)
    {
        unchecked // Overflow is fine, just wrap
        {
            int hash = 17;
            // Suitable nullity checks etc, of course :)
            hash = hash * 23 + obj.IDx.GetHashCode();
            hash = hash * 23 + obj.IDy.GetHashCode();
            hash = hash * 23 + obj.Name.GetHashCode();
            foreach (var kv in obj.Subs)
            {
                hash = hash * 23 + kv.Key.GetHashCode();
                hash = hash * 23 + subChecker.GetHashCode(kv.Value);
            }

            return hash;
        }
    }
}

public class SubCheck : IEqualityComparer<Sub>
{
    public bool Equals(Sub x, Sub y)
    {
        if (ReferenceEquals(x, y))
            return true;
        if (x == null || y == null)
            return false;
        if (x.Namex != y.Namex || x.Namey != y.Namey || x.Value != y.Value)
            return false;
        // check dictionary
        if (ReferenceEquals(x.Paths, y.Paths))
            return true;
        if (x.Paths == null || y.Paths == null || x.Paths.Count != y.Paths.Count)
            return false;
        foreach(var kv in x.Paths)
            if (!y.Paths.ContainsKey(kv.Key) || y.Paths[kv.Key] != kv.Value)
                return false;
        return true;
    }

    public int GetHashCode(Sub obj)
    {
        unchecked // Overflow is fine, just wrap
        {
            int hash = 17;
            // Suitable nullity checks etc, of course :)
            hash = hash * 23 + obj.Namex.GetHashCode();
            hash = hash * 23 + obj.Namey.GetHashCode();
            hash = hash * 23 + obj.Value.GetHashCode();
            foreach (var kv in obj.Paths)
            {
                hash = hash * 23 + kv.Key.GetHashCode();
                hash = hash*23 + kv.Value.GetHashCode();
            }

            return hash;
        }
    }
} 

这应该深入检查所有属性和字典。 然后,您可以使用以下循环将两个字典相互比较:

bool equal = true;
var allKeys = cont1.Keys.Concat(cont2.Keys).ToList();
var containerChecker = new ContainerCheck();

foreach (string key in allKeys)
{
    Container c1;
    Container c2;
    if (!cont1.TryGetValue(key, out c1) || !cont2.TryGetValue(key, out c2))
    {
        equal = false;
    }
    else
    {
        // deep check both containers
        if (!containerChecker.Equals(c1, c2))
            equal = false;
    }
    if(!equal)
        break;  // or collect differences
}

暂无
暂无

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

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