现有一个BSUserRoleResult类
public class BSUserRoleResult
{
/// <summary>
/// 获取或者设置 角色分组编号
/// </summary>
public int RoleGroupId { get; set; }
/// <summary>
/// 获取或者设置 角色编号
/// </summary>
public int RoleId { get; set; }
}
继承泛型接口 IEqualityComparer
/// <summary>
///
/// </summary>
public class BSUserRoleComparerResult : IEqualityComparer<BSUserRoleResult>
{
/// <summary>
///
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public bool Equals(BSUserRoleResult x, BSUserRoleResult y)
{
if (x == null || y == null)
return false;
if (x.RoleGroupId == y.RoleGroupId && x.RoleId==y.RoleId)
return true;
else
return false;
}
/// <summary>
///
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public int GetHashCode(BSUserRoleResult obj)
{
if (obj == null)
return 0;
else
return obj.RoleGroupId.GetHashCode() ^ obj.RoleId.GetHashCode();
}
}
调用方法
roleInfos = roleInfos.Distinct(new BSUserRoleComparerResult()).ToList();