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();
}
}

posted @ 2017-02-07 16:56  乐哈哈1  阅读(218)  评论(0编辑  收藏  举报