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 ...
随机推荐
- case when then else end 与 decode 的区别
case when then else end : 条件可以有 等于 ,大于 ,小于 与 decode : 条件只可以有等于的.
- 添加一个js扩展方法
String.prototype.repeatify=String.prototype.repeatify || function(times){ var str=''; for(var i=0;i& ...
- C++快速开发样本工程的建立--建立工程
因为QT建立工程清晰整洁,便于作为样板工程原型.采用QT 5.8.0 64位版本建立工程. 1.建立工程 打开VS2015 新建->新建项目->QT GUI Application -&g ...
- js节点操作实例
写了一个简单的小例子来引用js实例 1. 初步节点操作: 2.兼容性节点操作: 3.节点的类型,名字: 4.使用setAttribute设置属性 5.节点复制操作: 6.删除和替换节点 如有错误,还望 ...
- IAR新建MSP430工程
一.在IAR官网下载IAR for MSP430 软件 https://www.iar.com/iar-embedded-workbench/#!?architecture= 选择MSP430,然后 ...
- 回文词 (Palindromes,Uva401)
例题 3-3 回文词 (Palindromes,Uva401) 输入一个字符中,判断它是否为回文串以及镜像串.输入字符串保证不含数字0.所谓回文串,就是反转以后和原串相同,如abba和madam.所有 ...
- 一道hive面试题(窗口函数)
表student中的数据格式如下: name month degree s1 201801 As1 201802 As1 201803 Cs1 201804 As1 201805 As1 201806 ...
- R语言学习笔记(二十四):plyr包的用法
plyr 这个包,提供了一组规范的数据结构转换形式. Input/Output list data frame array list llply() ldply() laply() data fram ...
- 20155222 卢梓杰 myod
20155222 卢梓杰 myod 复习c文件处理内容 编写myod.c 用myod XXX实现Linux下od -tx -tc XXX的功能 main与其他分开,制作静态库和动态库 编写Makefi ...
- 201555334 实验一:Java开发环境的熟悉 总结
201555334 实验一:Java开发环境的熟悉 一.实验目的: 使用JDK编译.运行简单的Java程序: 使用Idea软件 编辑.编译.运行.调试Java程序. 二.实验内容: 编程实现让用户输入 ...