天真的我最开始以为可以写成list.distinct(x=>x.name);以为这样就可以按照name去重了,结果是不行的。这里记录下正确的用法。

1.这里是针对int集合  可以满足

#region 对数字集合的去重
//List<int> list = new List<int> { 1,1,2,3,4,5,5,6,6,7,7,7 };
//foreach (var item in list)
//{
// Console.Write(item+" ");
//}
//var res = list.Distinct();
//Console.WriteLine();
//Console.WriteLine("********************************");
//foreach (var item in res)
//{
// Console.Write(item+" ");
//}
//可以看到 对于数字来说 distinct可以实现去重
#endregion

2.对对象集合使用 失败

#region 对非数字类型去重失败的方法
//List<Student> list = new List<Student>
//{
// new Student{name="张三",age=20},
// new Student{name="张三",age=20},
// new Student{name="李四",age=20},
// new Student{name="李四",age=20},
// new Student{name="王五",age=20},
// new Student{name="赵六",age=20},
// new Student{name="陈启",age=20},
// new Student{name="陈启",age=20},
//};
//foreach (var item in list)
//{
// Console.Write(item.name + " ");
//}
//var res = list.Distinct();
//Console.WriteLine();
//Console.WriteLine("********************************");
//foreach (var item in res)
//{
// Console.Write(item.name + " ");
//}
//运行这段代码 可以看到去重失败了 下面来看正确的做法
#endregion

3.这才是正确的方法

class Program
{
static void Main(string[] args)
{
#region 对非数字类型去重正确
List<Student> list2 = new List<Student>
{
new Student{name="张三",age=20},
new Student{name="张三",age=20},
new Student{name="李四",age=20},
new Student{name="李四",age=20},
new Student{name="王五",age=20},
new Student{name="赵六",age=20},
new Student{name="陈启",age=20},
new Student{name="陈启",age=20},
};
foreach (var item in list2)
{
Console.Write(item.name + " ");
}
var res2 = list2.Distinct(new userCompare());
Console.WriteLine();
Console.WriteLine("********************************");
foreach (var item in res2)
{
Console.Write(item.name + " ");
}
//声明一个类 实现IEqualityComparer接口
//实现方法bool Equals(T x, T y);
//int GetHashCode(T obj);
//然后再内部写上比较的字段和属性 比较才能成功 才能去重
#endregion
Console.ReadLine();
}
}

public class Student
{
public int age { get; set; }
public string name { get; set; }
}

public class userCompare : IEqualityComparer<Student>
{

#region IEqualityComparer<user> Members

public bool Equals(Student x, Student y)
{

return x.name.Equals(y.name);

}

public int GetHashCode(Student obj)
{

return obj.name.GetHashCode();

}

#endregion

}

最新文章

  1. 下拉菜单demo---参考阿里云首页顶部下拉菜单
  2. bonext.js学习笔记
  3. Ext.encode 抛出异常“Uncaught RangeError: Maximum call stack size exceeded”
  4. DDD 领域驱动设计-看我如何应对业务需求变化?
  5. discuz ucenter通信失败
  6. Servlet &amp; JSP - HttpSession
  7. WordPress Complete Gallery Manager插件‘upload-images.php’任意文件上传漏洞
  8. 总结&amp;计划
  9. Codeforces Round #232 (Div. 1) A 解题报告
  10. [j2ee][IDEA properties中文乱码解决]
  11. CentOS 6.5下Percona Xtrabackup的安装错误解决方案
  12. hdu 1171 Big Event in HDU(母函数)
  13. 分享我的学习记录 svn地址
  14. SQL Server 文件操作
  15. django 前端模板继承显示model中使用choices的字段
  16. django2.1---后台管理 admin 字段内容过长,省略号替代
  17. ef 更新数据库
  18. SQL2005分页存储过程(支持多表联接)
  19. 【整理】HTML5游戏开发学习笔记(3)- 抛物线运动
  20. MySQL 报错

热门文章

  1. LiteOS-任务篇-源码分析-系统启动函数
  2. docker-管理容器常用命令
  3. tslib-1.4移植(转)
  4. 实验四  CSS样式的应用
  5. MeteoInfoLab脚本示例:创建netCDF文件(合并文件)
  6. 【编程学习】浅谈哈希表及用C语言构建哈希表!
  7. 如何把C++的源代码改写成C代码?而C改C++只需一步!
  8. Spring Aop 详解一
  9. spring boot:shardingsphere+druid整合seata分布式事务(spring boot 2.3.3)
  10. C# URL编码