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 get 3 rows, although I should get 1 record because 2 rows in assignedUsers is similar to assignUsers
var users = accountApp.GetUsersByAccountId(context.GetUserData().AccountId);
List<AssignUserViewModel> assignUsers = Mapper.Map<List<AssignUserViewModel>>(users).ToList();
var mailUsers = mailApp.GetMailAssignedByMailId(id).Select(m => new { m.UserId, m.User.Name }).ToList();
List<AssignUserViewModel> assignedUsers = mailUsers.Select(Mapper.DynamicMap<AssignUserViewModel>).ToList();
assignUsers = assignUsers.Except(assignedUsers).ToList();
In order to make
Exceptmethod working as expected, the classAssignUserViewModelmust haveGetHashCodeandEqualsmethods correctly overridden.For example, if
AssignUserViewModelobjects are uniquely defined by theirId, you should define the class in this way:Otherwise, if you can’t/don’t want to change the class implementation, you can implement an
IEqualityComparer<>and pass it to theExceptmethod, e.g. :then your last line would become: