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()函数 ...
随机推荐
- leetcode526
public class Solution { //回溯法 //根据回溯的思路,同样,可以对本题的Beautiful排列实现. //比如,当N为5时,使用回溯算法先是得到(1,2,3,4,5)排列,符 ...
- dclcommon200.bpl
xe6 dclcommon200.bpl xe7 dclcommon210.bpl xe8 dclcommon220.bpl xe7,xe8都有对应的文件,xe6为何没有?
- 我的Linux之路——实现虚拟机VMware上linux与windows互相复制与粘贴
出自:http://blog.csdn.net/u012243115/article/details/40454063 解决方法:只需要在CentOS安装一个vmware-tools的工具. 1.打开 ...
- Mysql 日志文件类型
简介: Mysql 中提供了多种类型的日志文件,分别反映 Mysql 的不同信息,了解它们很有必要. 1.Error log ( 错误日志 ) 错误日志记录了 Mysql Server 运行过程中所有 ...
- json和jsonp的区别(转)
原文链接:http://www.cnblogs.com/dowinning/archive/2012/04/19/json-jsonp-jquery.html 前言: 说到AJAX就会不可避免的面临 ...
- CMDB-客户端
配置文件的设置 大体思路: 1,通过开始文件将用户配置信息的文件放置到环境变量中. 2,在lib文件中的config文件中,从环境变量中获取到用户的配置,通过importlib模块导入用户配置文件,通 ...
- 字符串查找 · Implement strStr()
[抄题]: 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始).如果不存在,则返回 -1. 如果 ...
- 28-Truck History(poj1789最小生成树)
http://poj.org/problem?id=1789 Truck History Time Limit: 2000MS Memory Limit: 65536K Total Submiss ...
- ubuntu配置ftp
0.sudo apt-get install vsftpd 1.vi /etc/vsftpd.conf 将里面的注释打开 2.sudo service vsftpd restart 3.端口号21
- p2598 [ZJOI2009]狼和羊的故事
传送门 分析 起点向狼连边,羊向终点连边,边权均为inf 每个点向它四联通的点连边权萎1的边 跑最小割即可 代码 #include<iostream> #include<cstdio ...