标签:
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);
}
}
标签:
原文地址:http://www.cnblogs.com/niejun/p/4688600.html