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

时间:2017-08-15 16:45:06

标签: c# lambda hashset predicate removeall

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

    [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,Mayeye,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。大多数用户条目都是相同的,所以我不确定为什么它不会删除它们中的任何一个。

1 个答案:

答案 0 :(得分:1)

您必须覆盖用户中的Equals(object)GetHashCode()以定义使两个用户相等的内容,或者您​​必须编写实现IEqualityComparer<Users>的类并将该类传递给用户的构造函数你创建的HashSet。

当你只使用默认的相等实现时,就像你的代码当前一样,两个用户被认为是平等的唯一方法是Object.RefrenceEquals(user1, user2)返回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));