简体   繁体   English

比较两个相同类型的对象列表

[英]Compare two lists of objects of the same type

This is how the custom object is defined: 这是定义自定义对象的方式:

public class AccountDomain
{
    public string MAILDOMAIN { get; set; }
    public string ORG_NAME { get; set; }
}

This is how I am populating the List of objects: 这就是我填充对象列表的方式:

    List<AccountDomain> mainDBAccountDomain = mainDB.GetAllAccountsAndDomains();
    List<AccountDomain> manageEngineAccountDomain = ManageEngine.GetAllAccountsAndDomains();

This code works fine - if I look at the locals windows I can see a List of Objects in both mainDBAccountDomain and manageEngineAccountDomain. 这段代码运行良好-如果我查看本地窗口,则可以在mainDBAccountDomain和manageEngineAccountDomain中看到一个对象列表。

I'm struggling with the next bit, ideally I want a new list of type AccountDomain that contains all entries that are in mainDBAccountDomain and not ManageEngineAccountDomain 我正在努力下一点,理想情况下,我想要一个AccountDomain类型的新列表,其中包含mainDBAccountDomain中的所有条目,而不是 ManageEngineAccountDomain

Any help greatly appreciated, even if it's just a pointer in the right direction! 任何帮助都将不胜感激,即使它只是朝着正确方向的指针!

I want a new list of type AccountDomain that contains all entries that are in mainDBAccountDomain and not ManageEngineAccountDomain 我想要一个AccountDomain类型的新列表,其中包含mainDBAccountDomain中的所有条目,而不是 ManageEngineAccountDomain中的所有条目

It's very simple with linq to objects, it's exactly what the Enumerable.Except function does: 使用linq来处理对象非常简单,这正是Enumerable.Except函数的作用:

var result = mainDBAccountDomain.Except(manageEngineAccountDomain).ToList();

You can pass a comparer to the Except function if you need something different from reference equality, or you could implement Equals and GetHashCode in AccountDomain (and optionally implement IEquatable<AccountDomain> on top of these). 如果您需要与引用相等不同的其他功能,则可以将比较器传递给Except函数,或者可以在AccountDomain实现EqualsGetHashCode (并可以选择在这些之上实现IEquatable<AccountDomain> )。

See this explanation if you need more details about comparers. 这说明如果您需要了解comparers更多的细节。

Here's an example: 这是一个例子:

public class AccountDomainEqualityComparer : IEqualityComparer<AccountDomain>
{
    public static readonly AccountDomainEqualityComparer Instance
        = new AccountDomainEqualityComparer();

    private AccountDomainEqualityComparer()
    {
    }

    public bool Equals(AccountDomain x, AccountDomain y)
    {
        if (ReferenceEquals(x, y))
            return true;

        if (x == null || y == null)
            return false;

        return x.MAILDOMAIN == y.MAILDOMAIN
            && x.ORG_NAME == y.ORG_NAME;
    }

    public int GetHashCode(AccountDomain obj)
    {
        if (obj == null)
            return 0;

        return (obj.MAILDOMAIN ?? string.Empty).GetHashCode()
            ^ (397 * (obj.ORG_NAME ?? string.Empty).GetHashCode());
    }
}

Then, you use it like this: 然后,您可以像这样使用它:

var result = mainDBAccountDomain.Except(manageEngineAccountDomain, 
                                        AccountDomainEqualityComparer.Instance)
                                .ToList();

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

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