检查两个List是相等的

本文关键字:List 两个 检查 | 更新日期: 2023-09-27 17:51:15

我有两个list

ListA<Emp>ListB<Emp>都有1000条记录。

Emp是Employee Class的对象。下面是我的Employee

public class Employee
{
    int ID = 0;
    string Name = String.Empty;
    string Dept = String.Empty;
    string Address = String.Empty;
    int Age = 0;
    string Email = String.Empty;
}

我想验证两个列表是否相等。电磁脉冲对象可以按不同的顺序放置。此外,可能有几个Emp对象在两个列表中具有完全相同的信息。我也得核实一下。

我尝试排序列表,并使用SequenceEqual

进行比较
Enumerable.SequenceEqual(ListA.OrderBy(s => s), ListB.OrderBy(s => s)

我正在得到低于错误

At least one object must implement IComparable.
Exception Stack trace is as below 
   at System.Collections.Comparer.Compare(Object a, Object b)
   at System.Collections.Generic.ObjectComparer`1.Compare(T x, T y)
   at System.Linq.EnumerableSorter`2.CompareKeys(Int32 index1, Int32 index2)
   at System.Linq.EnumerableSorter`1.QuickSort(Int32[] map, Int32 left, Int32 right)
   at System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count)
   at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext()
   at System.Linq.Enumerable.SequenceEqual[TSource](IEnumerable`1 first, IEnumerable`1 second, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.SequenceEqual[TSource](IEnumerable`1 first, IEnumerable`1 second)

我如何实现这个?如果你们能给我提供最快的方法,那就更好了,因为List中的对象数量可能会增长到1000万。谢谢你的帮助!

EDIT:每个员工都必须在两个列表中,顺序无关紧要。但是,如果ListA包含相同的雇员对象5次(这意味着一些重复的条目),并且ListB包含该雇员对象4次,则ListA和ListB不相等。

检查两个List<T>是相等的

您可以使用SequenceEqual和自定义IEqualityComparer<Employee>:

class EmployeeComparer : IEqualityComparer<Employee>
{
    public bool Equals(Employee x, Employee y)
    {
        if (x == null || y == null) return false;
        bool equals = x.ID==y.ID && x.Name == y.Name && x.Dept == y.Dept 
            && x.Address == y.Address && x.Age == y.Age && x.Email == y.Email;
        return equals;
    }
    public int GetHashCode(Employee obj)
    {
        if (obj == null) return int.MinValue;
        int hash = 19;
        hash = hash + obj.ID.GetHashCode();
        hash = hash + obj.Name.GetHashCode();
        hash = hash + obj.Dept.GetHashCode();
        hash = hash + obj.Address.GetHashCode();
        hash = hash + obj.Age.GetHashCode();
        hash = hash + obj.Email.GetHashCode();
        return hash;
    }
}

现在它是如此简单:

listA.SequenceEqual(ListB, new EmployeeComparer());

如果顺序不重要,并且您只想知道是否所有员工都在两个列表中,您可以使用HashSet<Employee>.SetEquals来确定两个列表是否包含相同的人员:

var empComparer =  new EmployeeComparer();
bool bothEqual = new HashSet<Employee>(ListA, empComparer)
      .SetEquals(new HashSet<Employee>(ListB, empComparer));

最佳复杂度为O(N)下面使用HashSet实现:

类实现GetHashCode和Equals:

public class Employee
{
    public int ID = 0;
    public string Name = String.Empty;
    public string Dept = String.Empty;
    public string Address = String.Empty;
    public int Age = 0;
    public string Email = String.Empty;
    public override int GetHashCode()
    {
        return
            ID.GetHashCode() ^
            (Name ?? String.Empty).GetHashCode() ^
            (Dept ?? String.Empty).GetHashCode() ^
            (Address ?? String.Empty).GetHashCode() ^
            Age.GetHashCode() ^
            (Email ?? String.Empty).GetHashCode()
            ;
    }
    public override bool Equals(object obj)
    {
        Employee other = obj as Employee;
        if (obj == null)
            return false;
        return ID == other.ID &&
                Name == other.Name &&
                Dept == other.Dept &&
                Address == other.Address &&
                Age == other.Age &&
                Email == other.Email;
    }
}

比较列表的函数:

public static bool CompareLists(List<Employee> list1, List<Employee> list2)
{
    if (list1 == null || list2 == null)
        return list1 == list2;
    if (list1.Count != list2.Count)
        return false;
    Dictionary<Employee, int> hash = new Dictionary<Employee, int>();
    foreach (Employee employee in list1)
    {
        if (hash.ContainsKey(employee))
        {
            hash[employee]++;
        }
        else
        {
            hash.Add(employee, 1);
        }
    }
    foreach (Employee employee in list2)
    {
        if (!hash.ContainsKey(employee) || hash[employee] == 0)
        {
            return false;
        }
        hash[employee]--;
    }
    return true;
}

如果列表中的数字将变得非常大(10M),您可能不得不考虑并行化查找以获得可接受的查询时间。

考虑使用PLINQ

如果能更清楚地说明你所说的"相等"是什么意思就好了。等效性检查有多复杂?您是否检查对象是否相同,或者对象是否相同?

另一个需要考虑的是:如果元素的数量将变得很大,您是否可以考虑将此检查从。net移到数据库中—也许作为存储过程?

将列表缩减为标量类型:int, string, ....

L1.Select(x => x.K).ToArray()

使用except方法

L1.Select(x => x.K).ToArray().Except(L1.Select(x => x.K).ToArray())

如果结果集的计数为0,则List =

L1.Select(x => x.K).ToArray().Except(L1.Select(x => x.K).ToArray()).Count()
一起

public class Program {
    public static void Main(String[] args) {
        List<O> L1 = new List<O>{
            new O {K = 1, V = "abcd"},
            new O {K = 2, V = "efgh"}
        };
        List<O> L2 = new List<O>{
            new O {K = 1, V = "abcd"}
        };
        List<O> L3 = new List<O>{
            new O {K = 1, V = "abcd"},
            new O {K = 3, V = "ijkl"}
        };
        List<O> L4 = new List<O>{
            new O {K = 2, V = "efgh"},
            new O {K = 1, V = "abcd"}
        };
        Console.WriteLine(L1.Select(x => x.K).ToArray().Except(L1.Select(x => x.K).ToArray()).Count());
        Console.WriteLine(L1.Select(x => x.K).ToArray().Except(L2.Select(x => x.K).ToArray()).Count());
        Console.WriteLine(L1.Select(x => x.K).ToArray().Except(L3.Select(x => x.K).ToArray()).Count());
        Console.WriteLine(L1.Select(x => x.K).ToArray().Except(L4.Select(x => x.K).ToArray()).Count());
    }
} 
public class O {
    public int K { get; set; }
    public String V { get; set; }
}

就是这么说的。
在Employee
类上实现iccomparable还需要重写Equals
由于可能有大量的调用GetHashCode保存它,只计算更改。
测试

IComparable接口

public MainWindow()
{
    InitializeComponent();
    List<Person> PLa = new List<Person>();
    List<Person> PLb = new List<Person>();
    PLa.Add(new Person { Age = 3, Name = "Jim"});
    PLa.Add(new Person { Age = 2, Name = "Jimmmy" });
    PLa.Add(new Person { Age = 1, Name = "Jim" });
    PLb.Add(new Person { Age = 1, Name = "Jim" });
    PLb.Add(new Person { Age = 3, Name = "Jim" });
    PLb.Add(new Person { Age = 2, Name = "Jimmmy" });
    System.Diagnostics.Debug.WriteLine(ListSameIgnoreOrder(PLa, PLb));
}
public bool ListSameIgnoreOrder(List<Person> PLa, List<Person> PLb)
{
    if (PLa.Count != PLb.Count) return false;
    //PLa.Sort();
    //PLb.Sort();
    return Enumerable.SequenceEqual(PLa.OrderBy(s => s), PLb.OrderBy(s => s));
    //for (int i = 0; i < PLa.Count; i++)
    //{
    //    System.Diagnostics.Debug.WriteLine(
    //        PLa[i].Age.ToString() + " " + PLb[i].Age.ToString() + " " +
    //        PLa[i].Name + " " + PLb[i].Name);
    //    if (!PLa[i].Equals(PLb[i])) return false;
    //}
    //return true;
}
public class Person : object, IComparable
{
    private int age = 0;
    private string name = string.Empty;
    private int hash;
    public int Age
    {
        get { return age; }
        set 
        {
            if (age == value) return;
            age = value;
            CalcHash();
        }
    }
    public string Name
    {
        get { return name; }
        set 
        { 
            if (name == value) return;
            name = value;
            CalcHash();
        }
    }
    public override bool Equals(Object obj)
    {
        //Check for null and compare run-time types.
        if (obj == null || !(obj is Person)) return false;
        Person f = (Person)obj;
        if (f.Age != this.Age) return false;
        return (string.Compare(f.name, this.name) == 0);
    }
    private void CalcHash()
    {
        hash = Age.GetHashCode() ^
            (Name ?? String.Empty).GetHashCode();
    }
    public override int GetHashCode()
    {
        return hash;
        //return age ^ name.GetHashCode();
    }
    public int CompareTo(object obj)
    {
        if (obj == null) return 1;
        Person otherPerson = obj as Person;
        if (otherPerson != null)
        {
            if (otherPerson.Age > this.Age) return -1;
            if (otherPerson.Age < this.Age) return 1;
            // compare all properties like above
            return string.Compare(otherPerson.name, this.name);
        }
        else
            throw new ArgumentException("Object is not a Person");
    }
    public Person() { CalcHash(); }
}

可以了

public bool EqualList(Dictionary<int, string> a, Dictionary<int, string> b)
{
    if (a.Count == b.Count)
    {
        bool rs = false;
        foreach (var i in a)
        {
            if (b.ContainsKey(i.Key))
            {
                rs = true;
            }
            else
            {
                rs = false;
                break;
            }
        }
        return rs;
    }
    else
    {
        return false;
    }

用法:

if(EqualList(List<A>.ToDictionary(k => k.Key, k => k.Value), List<B>.ToDictionary(k => k.Key, k => k.Value)){
}else{
}