List<object>进行Distinct()去重
有时我们会对一个list<T>集合里的数据进行去重,C#提供了一个Distinct()方法直接可以点得出来。如果list<T>中的T是个自定义对象时直接对集合Distinct是达不到去重的效果。我们需要新定义一个去重的类并继承IEqualityComparer接口重写Equals和GetHashCode方法,如下Demo
using System;
using System.Collections.Generic;
using System.Linq; namespace MyTestCode
{
/// <summary>
/// Description of DistinctDemo.
/// </summary>
public class DistinctDemo
{
private static List<Student> list;
public DistinctDemo()
{
} public static void init()
{
list = new List<Student>{
new Student{
Id=,
Name="xiaoming",
Age=
},
new Student{
Id=,
Name="xiaohong",
Age=
},
new Student{
Id=,
Name="xiaohong",
Age=
},
};
} public void Display()
{
list = list.Distinct(new ListDistinct()).ToList();
foreach (var element in list) {
Console.WriteLine(element.Id +"/"+element.Name+"/"+element.Age);
}
} } public class Student
{
public int Id{get;set;}
public string Name{get;set;}
public int Age{get;set;}
} public class ListDistinct : IEqualityComparer<Student>
{
public bool Equals(Student s1,Student s2)
{
return (s1.Name == s2.Name);
} public int GetHashCode(Student s)
{
return s==null?:s.ToString().GetHashCode();
}
}
}
List<object>进行Distinct()去重的更多相关文章
- Linq 中的distinct去重
Linq的Distinct和T-Sql的distinct一样,可以将重复的结果集去重注意: 1 distinct去重记录要求每个字段都重复时,才算重复对象,这与sql一样2 distinct语句可以和 ...
- 存储过程系列三:根据表别名方式distinct去重插入
1.根据表别名方式distinct去重插入 insert into GG_XKZ_YLQXSCXKESL_SCDZ ( bzj, xkzid, sqid, jtdz, szsf, ...
- .NET-list扩展方法Distinct去重
原文链接:https://blog.csdn.net/daigualu/article/details/70800012 .NET中list的扩展方法Distinct可以去掉重复的元素,分别总结默认去 ...
- .Net Collection Distinct 去重
由于业务场景的需要,海量的数据需要进行处理.组装,难免会出现冗余的重复数据.如何处理重复的数据就是一个问题. 简单的集合中,去重就可以用linq distinct来完成.对于复杂的集合直接使用dist ...
- postgresql中使用distinct去重
select语法 [ WITH [ RECURSIVE ] with_query [, ...] ] SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ...
- DISTINCT 去重仍有重复的分析
logger日志报错 插入数据时违反主键唯一约束 org.springframework.dao.DuplicateKeyException: ### Error updating database. ...
- List<Object> 多条件去重
上一篇将到根据某一条件去重List<Object> 对象链表.本文章根据多条件去重List<Object>去重 private List<StaingMD0010> ...
- C# Distinct去重泛型List
List<int>去重 List<string>去重 List<T>去重 1. List<int>去重 List<int> ilist = ...
- 关于Django中的数据库操作API之distinct去重的一个误传
转载自http://www.360doc.com/content/18/0731/18/58287567_774731201.shtml django提供的数据库操作API中的distinct()函数 ...
随机推荐
- leetcode746
class Solution { public: int minCostClimbingStairs(vector<int>& cost) { vector<); totol ...
- prettytable模块(格式化打印内容)
1.查看系统是否已经安装prettytable模块 2.下载prettytable模块 登陆:https://pypi.python.org/pypi/PrettyTable 3.安装PrettyTa ...
- MySQL 复合索引
一. 1.索引越少越好,在修改数据时,第个索引都要进行更新,降低写速度.2.最窄的字段放在键的左边3.避免file sort排序,临时表和表扫描. 二.复合索引的建立原则: 如果您很可能仅对一个列多次 ...
- Linux ALSA声卡驱动之一:ALSA架构简介
声明:本博内容均由http://blog.csdn.net/droidphone原创,转载请注明出处,谢谢! 一. 概述 ALSA是Advanced Linux Sound Architecture ...
- 清除html中的标记,只留下文字
/// <summary>/// 清除html中的标记,只留下文字./// </summary>/// <param name="HTML">& ...
- MySQL数据库篇之索引原理与慢查询优化之一
主要内容: 一.索引的介绍 二.索引的原理 三.索引的数据结构 四.聚集索引与辅助索引 五.MySQL索引管理 六.测试索引 七.正确使用索引 八.联合索引与覆盖索引 九.查询优化神器--explai ...
- golang怎么使用redis,最基础的有效的方法
最近在学GO语言,我自己也喜欢使用redis,于是乎就顺便把go操作redis的方法也给学了,有个第三方包,在GitHub上面找的 go get github.com/alphazero/Go-Red ...
- 超出div宽度范围的文字进行省略号省略,在鼠标移上去以后显示完整的内容
一.前言 当我们在固定的范围内显示内容时,我们是希望能够完整显示的,然而往往事与愿违,文本会超出我们给定的范围,这时候怎么办呢? 二.超出范围,对文本进行省略号隐藏 先上图 代码很简单 div{ wi ...
- 从Oracle数据库中查询与某一时间点最接近的记录
select * from data_taskregionschedule WHERE regioncode='HYL' and updatetime-to_date('2018-05-15','yy ...
- MYSQL 创建常见问题
1.创建函数时,报错: 出错信息:ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL D ...