List.Except is not working

前端 未结 2 492
栀梦
栀梦 2020-11-30 14:06

I try to subtract 2 lists like below code, assignUsers has got 3 records and assignedUsers has got 2 rows. After Except method I still

2条回答
  •  [愿得一人]
    2020-11-30 14:35

    In order to make Except method working as expected, the class AssignUserViewModel must have GetHashCode and Equals methods correctly overridden.

    For example, if AssignUserViewModel objects are uniquely defined by their Id, you should define the class in this way:

    class AssignUserViewModel
    {
        // other methods...
    
    
        public override int GetHashCode()
        {
            return this.Id.GetHashCode();
        }
        public override bool Equals(object obj)
        {
            if (!(obj is AssignUserViewModel))
                throw new ArgumentException("obj is not an AssignUserViewModel");
            var usr = obj as AssignUserViewModel;
            if (usr == null)
                return false;
            return this.Id.Equals(usr.Id);
        }
    }
    

    Otherwise, if you can't/don't want to change the class implementation, you can implement an IEqualityComparer<> and pass it to the Except method, e.g. :

    class AssignUserViewModelEqualityComparer : IEqualityComparer
    {
        public bool Equals(AssignUserViewModel x, AssignUserViewModel y)
        {
            if (object.ReferenceEquals(x, y))
                return true;
            if(x == null || y == null)
                return false;
            return x.Id.Equals(y.Id);
        }
    
        public int GetHashCode(AssignUserViewModel obj)
        {
            return obj.Id.GetHashCode();
        }
    }
    

    then your last line would become:

    assignUsers = assignUsers.Except(assignedUsers, new AssignUserViewModelEqualityComparer()).ToList();
    

提交回复
热议问题