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
Why this happening?
When you use Set Operations (Distinct, Except, Intersect, Union) Linq need to compare sequence(s) elements for equality. By default Linq uses Object.Equals and Object.GetHashCode methods to compare elements. If these methods are not overridden in your type, then base class's implementation is used, which compare objects by reference equality. Default implementation guarantees that two objects that are the same reference have the same hash code (thus considered equal). This is your case. Mapper class creates new instances of AssignUserViewModel objects, which have different references, and cannot be treated as equal (even if all field values are the same).
So, what could we do with this?
Override Equals and GetHashCode methods in your class. It's up to you how you will treat objects equality - all fields, or just identity. Linq will use your methods to compare elements.
Provide your own comparer (this is usually case when you cannot modify your object and override Equals and GetHashCode. Yes, all Linq Set Operations have two overloads - one which uses default comparer, and other, which accepts yours IEqualityComparer
Use anonymous types. All anonymous types already have generated methods Equals and GetHashCode, which use comparison of all properties to determine if objects are equal. In this case you don't need neither to modify your type nor to create comparer.
Thus you already have samples of first two approaches, here is last one:
var assignUsers = accountApp.GetUsersByAccountId(context.GetUserData().AccountId)
.Select(u => new { u.UserId, u.Name });
var assignedUsers = mailApp.GetMailAssignedByMailId(id)
.Select(m => new { m.UserId, m.User.Name });
var assignUsers = assignUser.Except(assignedUsers);
// do not map until here
List result =
assignUsers.Select(Mapper.DynamicMap).ToList();