我有两个列表
List<clsEntity> usersWithNPIRCodingPermission // It has count 159
List<clsEntity> usersWithRaiseReq // It has count 219
当在这些上相交时(有55条记录,但我的下面的行变为空列表)
List<clsEntity> users = usersWithRaiseReq.Intersect(usersWithNPIRCodingPermission).ToList();
为什么生成的users
列表为空?
答案 0 :(得分:4)
如果你的类型不是原始的,你需要为你的类型实现一个等式比较器。 int
等......
public class UsersComparer : IEqualityComparer<clsEntity>
{
public bool Equals(clsEntity x, clsEntity y)
{
if (Object.ReferenceEquals(x, y)) return true;
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
return x.ID == y.ID; // or whatever you use to determine equality
}
public int GetHashCode(clsEntity x)
{
if (Object.ReferenceEquals(x, null)) return 0;
return x.ID.GetHashCode();
}
}
用法:
List<clsEntity> users = usersWithRaiseReq.Except(
usersWithNPIRCodingPermissionn, new UsersComparer()).ToList();