dr_twilight

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

posted on 2015-07-30 10:52  dr_twilight  阅读(669)  评论(0编辑  收藏  举报