使用 Removeall Lambda Hashset 从列表中删除列表项

Remove List items from List with Removeall Lambda Hashset

我正在使用 FileHelpers 解析 2 个包含活动目录用户信息的文本文件,用户 class 定义为:

    [DelimitedRecord(",")]

public class Users
{
    public string LogonName;

    public string DisplayName;

    public string FirstName;

    public string LastName;

    public string SamAccountName;

    public string Password;

    public string Identity;

}

CSV 文件包含 LogonName、DisplayName、FirstName、LastName、SamAccountName、Password、Identity MM0123,Mothhair Mayeye,Mathhair,Mayye,MM0123,secrtpwd,Group1

两个文件中的大多数用户都是相同的。大约有 6500 条记录。

我希望在运行之后:

        List<Users> UserListCur = new List<Users>();
        List<Users> UserListLR = new List<Users>();
        FileHelperEngine engine = new FileHelperEngine(typeof(Users));

        var records = engine.ReadFile(@"C:\users.csv");
        var lrrecords = engine.ReadFile(@"C:\lastrun.csv");

        foreach (Users record in records)
        {

            UserListCur.Add(record);
        }

        foreach (Users record in lrrecords)
        {

            UserListLR.Add(record);
        }


        UserListCur.RemoveAt(0);
        UserListLR.RemoveAt(0);


        var UserListLRhash = new HashSet<Users>(UserListLR);
        UserListCur.RemoveAll(x => UserListLRhash.Contains(x));

应从 UserListCur 中删除 UserListLR 中的条目,但 UserListCur 未更改。大多数用户条目都是相同的,所以我不确定为什么它不删除其中的任何一个。

您必须覆盖用户中的 Equals(object)GetHashCode() 以定义使两个用户相等的原因,或者您必须编写实现 IEqualityComparer<Users> 的 class 并传递 class 到您创建的 HashSet 的构造函数中。

当您像您的代码当前那样使用相等的默认实现时,两个用户被视为相等的唯一方法是 Object.RefrenceEquals(user1, user2) returns true。

下面是创建比较器并将其传入的示例。

public sealed class UsersEqualityComparer : IEqualityComparer<Users>
{
    public bool Equals(Users x, Users y)
    {
        if (ReferenceEquals(x, y)) return true;
        if (ReferenceEquals(x, null)) return false;
        if (ReferenceEquals(y, null)) return false;
        if (x.GetType() != y.GetType()) return false;
        return string.Equals(x.LogonName, y.LogonName) && string.Equals(x.DisplayName, y.DisplayName) &&
               string.Equals(x.FirstName, y.FirstName) && string.Equals(x.LastName, y.LastName) &&
               string.Equals(x.SamAccountName, y.SamAccountName) && string.Equals(x.Password, y.Password) &&
               string.Equals(x.Identity, y.Identity);
    }

    public int GetHashCode(Users obj)
    {
        if(ReferenceEquals(obj, null))
            throw new ArgumentNullException("obj");

        unchecked
        {
            var hashCode = (obj.LogonName != null ? obj.LogonName.GetHashCode() : 0);
            hashCode = (hashCode * 397) ^ (obj.DisplayName != null ? obj.DisplayName.GetHashCode() : 0);
            hashCode = (hashCode * 397) ^ (obj.FirstName != null ? obj.FirstName.GetHashCode() : 0);
            hashCode = (hashCode * 397) ^ (obj.LastName != null ? obj.LastName.GetHashCode() : 0);
            hashCode = (hashCode * 397) ^ (obj.SamAccountName != null ? obj.SamAccountName.GetHashCode() : 0);
            hashCode = (hashCode * 397) ^ (obj.Password != null ? obj.Password.GetHashCode() : 0);
            hashCode = (hashCode * 397) ^ (obj.Identity != null ? obj.Identity.GetHashCode() : 0);
            return hashCode;
        }
    }
}

并且您的代码更改为

    List<Users> UserListCur = new List<Users>();
    List<Users> UserListLR = new List<Users>();
    FileHelperEngine engine = new FileHelperEngine(typeof(Users));

    var records = engine.ReadFile(@"C:\users.csv");
    var lrrecords = engine.ReadFile(@"C:\lastrun.csv");

    foreach (Users record in records)
    {

        UserListCur.Add(record);
    }

    foreach (Users record in lrrecords)
    {

        UserListLR.Add(record);
    }


    UserListCur.RemoveAt(0);
    UserListLR.RemoveAt(0);


    var UserListLRhash = new HashSet<Users>(UserListLR, new UsersEqualityComparer());
    UserListCur.RemoveAll(x => UserListLRhash.Contains(x));