有时我们会对一个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()去重的更多相关文章

  1. Linq 中的distinct去重

    Linq的Distinct和T-Sql的distinct一样,可以将重复的结果集去重注意: 1 distinct去重记录要求每个字段都重复时,才算重复对象,这与sql一样2 distinct语句可以和 ...

  2. 存储过程系列三:根据表别名方式distinct去重插入

    1.根据表别名方式distinct去重插入 insert into GG_XKZ_YLQXSCXKESL_SCDZ           ( bzj, xkzid,  sqid, jtdz, szsf, ...

  3. .NET-list扩展方法Distinct去重

    原文链接:https://blog.csdn.net/daigualu/article/details/70800012 .NET中list的扩展方法Distinct可以去掉重复的元素,分别总结默认去 ...

  4. .Net Collection Distinct 去重

    由于业务场景的需要,海量的数据需要进行处理.组装,难免会出现冗余的重复数据.如何处理重复的数据就是一个问题. 简单的集合中,去重就可以用linq distinct来完成.对于复杂的集合直接使用dist ...

  5. postgresql中使用distinct去重

    select语法 [ WITH [ RECURSIVE ] with_query [, ...] ] SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ...

  6. DISTINCT 去重仍有重复的分析

    logger日志报错 插入数据时违反主键唯一约束 org.springframework.dao.DuplicateKeyException: ### Error updating database. ...

  7. List<Object> 多条件去重

    上一篇将到根据某一条件去重List<Object> 对象链表.本文章根据多条件去重List<Object>去重 private List<StaingMD0010> ...

  8. C# Distinct去重泛型List

    List<int>去重 List<string>去重 List<T>去重 1. List<int>去重 List<int> ilist = ...

  9. 关于Django中的数据库操作API之distinct去重的一个误传

    转载自http://www.360doc.com/content/18/0731/18/58287567_774731201.shtml django提供的数据库操作API中的distinct()函数 ...

随机推荐

  1. druid 连接池的配置

    dataSource配置   <!-- 基于Druid数据库链接池的数据源配置 --> <bean id="dataSource" class="com ...

  2. 数据库执行的时候报ORA-01653错误

    查明原因是因为表空间文件到达了32G,因为oracle11g单个表空间大于32G的时候就不会自动在扩展了于是需要增加新的表空间文件,下面是4种解决此问题的方法 Meathod1:给表空间增加数据文件 ...

  3. Python OrderedDict使用

    一.最近最少使用实现: import collections class LRUDict(object): ''' 最近最少使用队列实现,最近使用的键值放后面 ''' def __init__(sel ...

  4. Transform & Physics

    [Transform & Physics] 1.Space.Unity定义了Space枚举值,此值如下: 通常通过Space.World.Space.Self来区别一个Vector是按世界坐标 ...

  5. gvim

    [gvim] 1.gvim的配置文件在安装目录下,文件名为_vimrc. 2.通过以下命令选择配色方案:

  6. 全部物料的交期都要加上两天 V_OUT_PR

    DUE_DATETIME加上两天就可以,如果只是部分物料的话,就要根据物料组或者别的一些条件去判断

  7. 147. Insertion Sort List (List)

    Sort a linked list using insertion sort. class Solution { public: ListNode *insertionSortList(ListNo ...

  8. Opencv3 图片膨胀与腐蚀

    #include <iostream>#include <opencv2/opencv.hpp> using namespace std;using namespace cv; ...

  9. 297. Serialize and Deserialize Binary Tree二叉树的序列化和反序列化(就用Q)

    [抄题]: Serialization is the process of converting a data structure or object into a sequence of bits ...

  10. 257. Binary Tree Paths返回所有深度优先的遍历

    [抄题]: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...