简体   繁体   English

如何比较两个包含包含字典的对象的列表?

[英]How to compare two lists that contain objects that contain Dictionaries?

I have two Lists (List “A” and List “B”) that hold objects of type “KeyStore”, which is shown below: 我有两个列表(列表“ A”和列表“ B”),其中包含“ KeyStore”类型的对象,如下所示:

public class KeyStore
{
    public Dictionary<string, string> PrimaryKeys { get; set; }

    public KeyStore(string pkName, string pkValue)
    {
        PrimaryKeys = new Dictionary<string, string> {{pkName, pkValue}};
    }

    public KeyStore()
    {
        PrimaryKeys = new Dictionary<string, string>();
    }
}

I need to look at each record in List “A” and see if there is a matching record in List “B”. 我需要查看列表“ A”中的每个记录,并查看列表“ B”中是否有匹配的记录。 If there is, then this record needs to be stored in a new list that contains just matching records. 如果存在,则此记录需要存储在仅包含匹配记录的新列表中。 A match is considered true if a record's PrimaryKeys dictionary contains the same number of entries and the same key value combination as a record in List “B”. 如果记录的PrimaryKeys词典包含与列表“ B”中的记录相同的条目数和相同的键值组合,则认为匹配为true。 The order of the entries in the dictionary is not important in testing for equality. 字典中条目的顺序对于测试是否相等并不重要。 If there is a record in List “A” that does not have a match in List “B”, then this needs to be stored in a new list that will only contain records found in List “A”. 如果列表“ A”中存在与列表“ B”不匹配的记录,则需要将其存储在仅包含在列表“ A”中找到的记录的新列表中。

Previously I did something similar when I had Lists of strings where I used “Intersect” and “Except” to create lists of matched and non-matched records. 以前,当我有字符串列表时,我使用“ Intersect”和“ Except”来创建匹配和不匹配记录的列表时,我做类似的事情。 I'm assuming that now that I need to compare these KeyStore objects I need to go up a level of complexity. 我假设现在需要比较这些KeyStore对象,因此我需要提高复杂性。 Can anyone offer a solution or advise on how I should approach this problem? 谁能提供解决方案或建议我该如何解决此问题?

EDIT 1 ---------------- 编辑1 ----------------

Based on comments, I have created a class that implements IEqualityComparer, as shown below: 基于注释,我创建了一个实现IEqualityComparer的类,如下所示:

class KeyStoreComparer : IEqualityComparer<KeyStore>
{
    public bool Equals(KeyStore x, KeyStore y)
    {
        if (x != null && x.PrimaryKeys.Count == y.PrimaryKeys.Count)
        {
            return x.PrimaryKeys.Keys.All(k => y.PrimaryKeys.ContainsKey(k)) &&
                   x.PrimaryKeys.Keys.All(k => x.PrimaryKeys[k].Equals(y.PrimaryKeys[k]));
        }
        return false;
    }

    public int GetHashCode(KeyStore obj)
    {
        return ReferenceEquals(obj, null) ? 0 : obj.GetHashCode();
    }
}

I have created some dummy data but when the "Intersect" command is run the above code is never called. 我已经创建了一些虚拟数据,但是当运行“相交”命令时,从未调用以上代码。 Any ideas where I am going wrong? 有什么想法我要去哪里吗?

var ListA = new List<KeyStore>();
ListA.Add(new KeyStore("a", "b"));
ListA.Add(new KeyStore("c", "d"));

var ListB = new List<KeyStore>();
ListB.Add(new KeyStore("a", "b"));
ListB.Add(new KeyStore("x", "y"));

var g = ListA.Intersect(ListB, new KeyStoreComparer());

The code in the "Equals" and "GetHashCode" may not be correct but I'm just trying to get it to get as far as running it before I can improve it. “ Equals”和“ GetHashCode”中的代码可能不正确,但是我只是想让它尽可能地运行,然后才能对其进行改进。

EDIT 2 --------------------------------------- 编辑2 ---------------------------------------

I have made various changes to the KeyStore class as shown in the example by “fox” on this page. 我已经对KeyStore类进行了各种更改,如本页中的示例“ fox”所示。 I still don't get the overridden functions to be called. 我仍然没有被调用的重写函数。 As an experiment I tried this: 作为实验,我尝试了以下操作:

var result = ListA.Equals(ListB);

When I do this the overridden functions in the KeyStor class don't run. 当我这样做时,KeyStor类中的重写功能不会运行。 But if I do this: 但是,如果我这样做:

var result = ListA[0].Equals(ListB[0]);

The overridden functions do run and give the expected result. 重写的函数可以运行并给出预期的结果。 Anyone know how I can get this to work for all items in the lists rather than just for individual records? 有谁知道我如何使它适用于列表中的所有项目,而不仅仅是单个记录?

EDIT 3 --------------------------------------- 编辑3 ---------------------------------------

The problem I am seeing is that the override works fine for single items, eg: 我看到的问题是,该替代对于单个项目工作正常,例如:

var a = new KeyStore("a", "b");
var b = new KeyStore("a", "b");
var c = a.Equals(b);

When I run the above my break point on the KeyStore "Equals" function is hit. 当我在上面运行时,我在KeyStore“等于”功能上的断点被击中。 As soon as I try to do something similar but with a List of KeyStore, the breakpoint is no longer hit. 只要我尝试执行类似的操作但使用KeyStore列表,就不再会遇到断点。 Do I need to do something extra when working with Lists? 使用列表时,我需要做些额外的事情吗?

public class KeyStore
{
    public Dictionary<string, string> PrimaryKeys { get; set; }

    public KeyStore(string pkName, string pkValue)
    {
        PrimaryKeys = new Dictionary<string, string> { { pkName, pkValue } };
    }

    public KeyStore()
    {
        PrimaryKeys = new Dictionary<string, string>();
    }

    public override bool Equals(object obj)
    {
        // If parameter is null return false.
        if (obj == null)
            return false;

        // If parameter cannot be cast to KeyStore return false.
        KeyStore targetKeyStore = obj as KeyStore;
        if (targetKeyStore == null)
            return false;

        return PrimaryKeys.OrderBy(pk => pk.Key).SequenceEqual(targetKeyStore.PrimaryKeys.OrderBy(pk => pk.Key));
    }

    public override int GetHashCode()
    {
        StringBuilder content = new StringBuilder();

        foreach (var item in PrimaryKeys.OrderBy(pk => pk.Key))
            content.AppendFormat("{0}-{1}", item.Key, item.Value);

        return content.ToString().GetHashCode();
    }
}

Eric Lippert's guide on implementing GetHashCode wrote that "equal items have equal hashes" . 埃里克·利珀特(Eric Lippert)关于实现GetHashCode的指南中写道: “相等的项目具有相等的哈希值” GetHashCode() implementation above just to show concept, might not suitable for production code. 上面的GetHashCode()实现只是为了展示概念,可能不适合生产代码。

Overriding the ToString method will help simplify your code quite a bit. 覆盖ToString方法将大大简化您的代码。 See if this helps: 看看是否有帮助:

public class KeyStore
{
    public SortedDictionary<string, string> PrimaryKeys
    {
        get;
        set;
    }

    public KeyStore(string pkName, string pkValue)
    {
        PrimaryKeys = new SortedDictionary<string, string> { { pkName, pkValue } };
    }

    public KeyStore()
    {
        PrimaryKeys = new SortedDictionary<string, string>();
    }

    public override bool Equals(object obj)
    {
        if(obj == null || (KeyStore)obj == null)
            return false;
        KeyStore temp = (KeyStore)obj;

        return ToString() == temp.ToString();
    }

    public override int GetHashCode()
    {
        return ToString().GetHashCode();
    }
    public override string ToString()
    {
        return PrimaryKeys.Count.ToString() + " : \n" + string.Join("\n",(from kvp in PrimaryKeys
                                                                          let s = kvp.Key + " - " + kvp.Value
                                                                          select s));
    }
}

List<KeyStore> Lista = new List<KeyStore>
{
    new KeyStore("testa","testa1"),
    new KeyStore("testb","testb1"),
    new KeyStore("testc", "testc1")
};
List<KeyStore> Listb = new List<KeyStore>
{
    new KeyStore("testa","testa1"),
    new KeyStore("testd","testb1"),
    new KeyStore("testc", "testa1"),
    new KeyStore("teste", "teste1")
};
var Listc = Lista.Intersect(Listb).ToList();
var Listd = Lista.Except(Listb).ToList();

?Listc
Count = 1
    [0]: {1 : 
testa - testa1}

?Listd
Count = 2
    [0]: {1 : 
testb - testb1}
    [1]: {1 : 
testc - testc1}

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

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