(linq) EF怎么去重复
tb1表有a,b,c,d,e五个字段
如果b,c,d三个字段都相同就视为重复.
怎么去重复只保留一条?
group by 我只会分组1个字段
------解决方案--------------------1. group by 可以分组N个字段: group x by new {x.id,x.name,x....}
2. Distinct 方法有个重载方法,重写IEqualityComparer来实现去重复,参考:
http://msdn.microsoft.com/en-us/library/bb338049.aspx
------解决方案--------------------方法一:分组N个字段
list.GroupBy(x => new { x.b, x.c,x.d }).Select(…);
方法二:构造Distinct比较器,例
public class UserComparer : IEqualityComparer<UserModels>
{
public bool Equals(UserModels t1, UserModels t2)
{
return (t1.b== t2.b && t1.c== t2.c && && t1.d== t2.d);
}
public int GetHashCode(UserModels obj)
{
return obj.ToString().GetHashCode();
}
}
//调用
var list = list.Distinct(new UserComparer()).ToList()