码迷,mamicode.com
首页 > 其他好文 > 详细

lambda distinct

时间:2015-07-30 12:59:11      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

public ActionResult Index()
{
IList<RegisterModel> regList = new List<RegisterModel>()
{
new RegisterModel{ UserName ="zhangsan", Password="123456", ConfirmPassword ="123456"},
new RegisterModel{ UserName ="li", Password="123456", ConfirmPassword ="123456"},
new RegisterModel{ UserName ="zhangsan", Password="123456", ConfirmPassword ="123456"},
new RegisterModel{ UserName ="wangwu", Password="123456", ConfirmPassword ="123456"},
new RegisterModel{ UserName ="zhangsan", Password="123456", ConfirmPassword ="123456"}
};
List<RegisterModel> lambdaList = regList.Distinct(new Compare<RegisterModel>(
(x, y) => (null != x && null != y) && (x.UserName == y.UserName))).ToList();


return View();
}
public delegate bool EqualsComparer<T>(T x, T y);
public class Compare<T> : IEqualityComparer<T>
{
private EqualsComparer<T> _equalsComparer;
public Compare(EqualsComparer<T> equalsComparer)
{
this._equalsComparer = equalsComparer;
}
public bool Equals(T x, T y)
{
if (null != this._equalsComparer)
return this._equalsComparer(x, y);
else
return false;
}
public int GetHashCode(T obj)
{
return obj.ToString().GetHashCode();
}
}

//////////////////******************************////////

var linqlist = (from r in regList
group r by r.UserName into g
select g).ToList();
foreach (var group in linqlist)
{
// 输出组内成员
foreach (var score in group)
{
Console.WriteLine(score);
}
}

lambda distinct

标签:

原文地址:http://www.cnblogs.com/niejun/p/4688600.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!