linq 去重
var list = new List<UserSecretReplyReqViewModel>()
{
new UserSecretReplyRepViewModel()
{
SecretReplyID = Guid.Empty,
AddTime = DateTime.Now.AddSeconds(1)
},
new UserSecretReplyRepViewModel(){
SecretReplyID = Guid.Empty,
AddTime = DateTime.Now.AddSeconds(2)
},
new UserSecretReplyRepViewModel(){
SecretReplyID = Guid.NewGuid(),
AddTime = DateTime.Now.AddSeconds(3)
}
};
list = list.Distinct(new UserSecretReplyComparer()).ToList();
class UserSecretReplyComparer : IEqualityComparer<UserSecretReplyReqViewModel>
{
public bool Equals(UserSecretReplyReqViewModel x, UserSecretReplyReqViewModel y)
{
if (x == null)
return y == null;
return x.SecretReplyID == y.SecretReplyID;
}
public int GetHashCode(UserSecretReplyReqViewModel obj)
{
if (obj == null)
return 0;
return obj.SecretReplyID.GetHashCode();
}
}