List<T>.Distinct()
)
};
//使用匿名方法
List<Person> delegateList = personList.Distinct(new Compare<Person>(
delegate(Person x, Person y)
{
if (null== x ||null== y) returnfalse;
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));
}
}
publicclass Person
{
publicint ID { get; set; }
publicstring Name { get; set; }
public Person(int id)
{
this.ID = id;
}
}
publicdelegatebool EqualsComparer<T>(T x, T y);
publicclass Compare<T> : IEqualityComparer<T>
{
private EqualsComparer<T> _equalsComparer;
public Compare(EqualsComparer<T> equalsComparer)
{
this._equalsComparer = equalsComparer;
}
publicbool Equals(T x, T y)
{
if (null!=this._equalsComparer)
returnthis._equalsComparer(x, y);
else
returnfalse;
}
publicint GetHashCode(T obj)
{
return obj.ToString().GetHashCode();
}
}
List<T>.Distinct()的更多相关文章
- [LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
Given a string S, find the length of the longest substring T that contains at most two distinct char ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- SQL中distinct的用法
SQL中distinct的用法 1.作用于单列 2.作用于多列 3.COUNT统计 4.distinct必须放在开头 5.其他 在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出 ...
- Oracle 查询语句(where,order by ,like,in,distinct)
select * from production;alter table production add productionprice number(7,2); UPDATE production s ...
- mysql中distinct的用法
本事例实验用表task,结构如下 MySQL> desc task; +-------------+------------+------+-----+-------------------+- ...
- Distinct Subsequences
https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...
- distinct 与 group by 去重
例如下表格:表名:fruit id Name Price Num 1 西瓜 10 2 2 西瓜 11 2 3 香蕉 10 3 4 桃子 10 2 当我想获取Name不重复的数据,结果如下 id Nam ...
- 大数据下的Distinct Count(二):Bitmap篇
在前一篇中介绍了使用API做Distinct Count,但是精确计算的API都较慢,那有没有能更快的优化解决方案呢? 1. Bitmap介绍 <编程珠玑>上是这样介绍bitmap的: B ...
- 扩展lamda表达中distinct按照字段去除重复
首先,我们定义一个Student类来测试. public class Student { public int ID { get; set; } public string Name { get; s ...
随机推荐
- Selenium报错整理
1. driver不匹配(常见于打不开浏览器,或者浏览器能打开但是获取不了网页元素,或者无法sendKey等问题) Exception in thread "main" org.o ...
- [iOS]app的生命周期
对于iOS应用程序,关键的是要知道你的应用程序是否正在前台或后台运行.由于系统资源在iOS设备上较为有限,一个应用程序必须在后台与前台有不同的行为.操作系统也会限制你的应用程序在后台的运行,以提高电池 ...
- python_frm组件
一.URL添加 from django.contrib import admin from django.urls import path,re_path from app01 import view ...
- vue面试题!!!
由于公司需要,需要把项目拆分,前端使用vue框架.最近面试vue总结的试题 1:mvvm框架是什么?它和其他框架的区别是什么? mvvm 全称model view viewModel,model数据模 ...
- 【2008-2009 ICPC NEERC D】Deposits(暴力)
题目链接 题意: 给你n个数a[i],m个数b[i],求出有几对数满足a[i]能整除b[i]. 思路: 直接暴力,先将第一组数存入a数组,第二组数存入b数组,然后在第二组数中遍历它整数倍的数(在10^ ...
- ABAP术语-Update Task
Update Task 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/20/1114184.html Part of an ABAP pro ...
- ubuntu SDL2 安装时依赖文件导致安装失败
今天打算学习littlev GUI,使用Ubuntu来实现仿真,然后在安装SDL2的时候,始终因为依赖关系导致安装失败,我尝试手动去安装那些有依赖关系的包发现根本不可行,然后我百度上也没有找到合适的法 ...
- BugFree后台统计Bug信息
以下为二维表信息 //统计严重等级Bug SELECT severity,count(severity) FROM `bf_bugview` where product_id=476 GROUP BY ...
- 那些年我们追过的C#奇葩关键字——忐忑
说到中国的歌坛,不能光说张学友这种大咖吧,我看那些怪咖更给力,比如我们的龚琳娜童鞋,一首神曲<忐忑>唱的那叫不可收拾,而且听到的改编版本更多,每一次都是心怀忐忑,就像C#里的那些关键字 说 ...
- 20155213 实验一《Java开发环境的熟悉》实验报告
20155213 实验一<Java开发环境的熟悉>实验报告 一. 实验内容及步骤 (一)使用JDK编译.运行简单的java程序 命令行下的程序开发 输入cd Code进入Code文件夹里 ...