class ListDistinctDemo
{
static void Main(string[] args)
{
List<Person> personList = new List<Person>(){
new Person(),//重复数据
new Person(),
new Person(),
new Person()
};
//使用匿名方法
List<Person> delegateList = personList.Distinct(new Compare<Person>(
delegate(Person x, Person y)
{
if (null == x || null == y) return false;
return x.ID == y.ID;
})).ToList();
delegateList.ForEach(s => Console.WriteLine(s.ID));
//使用 Lambda 表达式
List<Person> lambdaList = personList.Distinct(new Compare<Person>(
(x, y) => (null != x && null != y) && (x.ID == y.ID))).ToList();
lambdaList.ForEach(s => Console.WriteLine(s.ID));
//排序
personList.Sort((x, y) => x.ID.CompareTo(y.ID));
personList.ForEach(s => Console.WriteLine(s.ID));
}
}
public class Person
{
public int ID { get; set; }
public string Name { get; set; } public Person(int id)
{
this.ID = id;
}
}
public delegate bool EqualsComparer<T>(T x, T y);
public class Compare<T> : IEqualityComparer<T>
{
private EqualsComparer<T> _equalsComparer;
public Compare(EqualsComparer<T> equalsComparer)
{
this._equalsComparer = equalsComparer;
}
public bool Equals(T x, T y)
{
if (null != this._equalsComparer)
return this._equalsComparer(x, y);
else
return false;
}
public int GetHashCode(T obj)
{
return obj.ToString().GetHashCode();
}
}

C# List使用District去重复数据的更多相关文章

  1. LibreOffice去重复数据

    菜单:数据--更多筛选---标准筛选 点开“选项”,勾上“无重复值”. 然后还可以把结果复制到其它单元格.

  2. oracle 数据库去重复数据

    delete from 表名 a where rowid !=(select max(rowid) from 表名 b where a.ORDER_ID=b.ORDER_ID) 例:如果重复的数据表是 ...

  3. C# DataTable 去重复数据方法

    //获取dt中Id,Value 2个字段不重复的数据 newDt = dt.DefaultView.ToTable(true, "Id","Value" );

  4. List去重复数据

    for ( int i = 0 ; i < list.size() - 1 ; i ++ ) {  for ( int j = list.size() - 1 ; j > i; j -- ...

  5. js去重复和取重复数据

    js数组中取重复数据的方法: 方法一:去重复数据 <script> Array.prototype.distinct=function(){ var a=[],b=[]; for(var ...

  6. php数组去重复代码

    php数组去重复数据示例,有时候获得的php数组中总是出现value重复的,使用下面的方法就可以去掉重复数据 以数字开头的重复数据如: Array (  [0] => 100  [k1] =&g ...

  7. 关系数据库SQL之高级数据查询:去重复、组合查询、连接查询、虚拟表

    前言 接上一篇关系数据库SQL之基本数据查询:子查询.分组查询.模糊查询,主要是关系型数据库基本数据查询.包括子查询.分组查询.聚合函数查询.模糊查询,本文是介绍一下关系型数据库几种高级数据查询SQL ...

  8. distinct 去重复查询——两个表join 连接,去掉重复的数据

    ------distinct 去重复查询 select * from  accounts acc join (select distinct accid from roles) r on r.acci ...

  9. Oracle查询字符串数据进行排序,以及去重复

    原本的的一张表,填写数据的字段为字符串varchar2类型,然后进行排序的时候,就会出现问题.会默直接默认判断为第一个数字9最大,而不判断整个数字的大小. 所以,就要用到TO_NUMBER函数 sel ...

随机推荐

  1. OCP读书笔记(2) - 配置恢复

    RMAN的命令类型 1. sqlplus命令 [oracle@oracle admin]$ export ORACLE_SID=orcl [oracle@oracle admin]$ rman tar ...

  2. Android在ListView显示图片(重复混乱闪烁问题)

    Android在ListView显示图片(重复混乱闪烁问题) 1.原因分析 ListView item缓存机制: 为了使得性能更优,ListView会缓存行item(某行相应的View). ListV ...

  3. Wix打包系列(七) 添加系统必备组件的安装程序

    原文:Wix打包系列(七) 添加系统必备组件的安装程序 我们知道在vs的打包工程中添加系统必备组件是一件很容易的事情,那么在wix中如何检测系统必备组件并在安装过程中安装这些组件.这里以.Net Fr ...

  4. ZOJ 1584:Sunny Cup 2003 - Preliminary Round(最小生成树&amp;&amp;prim)

    Sunny Cup 2003 - Preliminary Round April 20th, 12:00 - 17:00 Problem E: QS Network In the planet w-5 ...

  5. 命令含执行JAVA程序

    1.当类没有包名时 javac Test.java java Test   2.当有包名情况下 package com.me.Test; javac -d . Test.java java com.m ...

  6. dell服务器从硬盘导入阵列信息

    前几天去南京客户那里更新新的业务系统,客户要求将服务器上的旧的硬盘拆下来,换上新的硬盘,重新做raid,客户自己要插入旧的硬盘读取旧数据,昨天做了几个实验,两台Dell R710服务器各4块硬盘,一台 ...

  7. A Game of Thrones(7) -Arya

    Arya’s stitches were crooked again. She frowned down at them with dismay and glanced over to where h ...

  8. linux--关于shell的介绍

    下面是最近学习shell的一些知识点总结***博客园-邦邦酱好*** 1.什么是shell(1)Shell将我们输入的指令与Kernel沟通,好让Kernel可以控刢硬件来正确无误地工作.(2)我们总 ...

  9. openwrt教程 第一章 物联网&amp;openwrt开发概述

    1.1 我们的宗旨 互联网.移动互联网的时代已经过去,物联网的时代已经来临!2014年,是物联网元年,2016年,物联网将达到高潮!为了迎接该潮流,我们工作室(F403科技创意室:http://f40 ...

  10. java 字符串 asc 加密解密

    package com; public class MD5Test { /** * @param args */ public static void main(String[] args) { Sy ...