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()函数 ...
随机推荐
- ubuntu下永久修改DNS
通过修改: sudo vi /etc/resolvconf/resolv.conf.d/base(这个文件默认是空的) 在里面插入: nameserver 8.8.8.8 nameserver 8.8 ...
- Sublime Text:初学者不知道的那些事
来源:Duchessjojo@译言 我是Sublime Text代码编辑器的忠实粉丝.我和诸多Mac程序员一样,最初使用的是Textmate代码编辑器.在Sublime Text 2发行后,我才开始转 ...
- vertex shader(2)
一次只有一个vertex shader是活跃的.你可以有多个vertex shader,如果一个物体特殊的变换或者灯光,你可以选择合适的vertex shader来完成这个任务. 你可能想使用vert ...
- python:find()函数,字符串查询
#find函数 # b中有a的元素则打印a对应的元素坐标(索引),否则打印-1 a = "abcd" b = "d" print(a.find(b))
- elasticsearch 安装 head插件
head插件 切换到Elasticsearch的安装目录 cd ~/demo/elasticsearch/es1 执行安装命令 [root@localhost es1]# bin/plugin ins ...
- java算法 第七届 蓝桥杯B组(题+答案) 9.取球博弈
9.取球博弈 (程序设计) 两个人玩取球的游戏.一共有N个球,每人轮流取球,每次可取集合{n1,n2,n3}中的任何一个数目.如果无法继续取球,则游戏结束.此时,持有奇数个球的一方获胜.如果两人都是 ...
- kalman处理realsense数据
代码来自:https://www.cnblogs.com/zjuhjm/archive/2012/12/29/2838472.html import numpy as npimport matplot ...
- Opencv HOG特征检测
HOGDescriptor hogDescriptor = HOGDescriptor(); hogDescriptor.setSVMDetector(hogDescriptor.getDefault ...
- Emgu CV的配置以及在VS 2012中进行图像处理的步骤和实例
说明: 1.所使用的Emgu CV是目前的最新版本3.1.0,下载链接为:https://sourceforge.net/projects/emgucv/files/emgucv/3.1.0/(我选的 ...
- ros console_bridge找不到
Could not find a package configuration file provided by "console_bridge" with any of the f ...