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 ...
随机推荐
- 百度地图中找不到BMap的解决
一般情况下是引用的问题,产生的原因大概有两种 1.不同架构的引用方式不同,引用js的方式不同导致 2.自身调用顺序有误 官方的引用方式是使用标签引入,示例 <script type=" ...
- #leetcode刷题之路25- k个一组翻转链表
给出一个链表,每 k 个节点一组进行翻转,并返回翻转后的链表.k 是一个正整数,它的值小于或等于链表的长度.如果节点总数不是 k 的整数倍,那么将最后剩余节点保持原有顺序. 示例 :给定这个链表:1- ...
- TopJUI Combobox 联动
这里给联动进行一个简单定义:因Combobox选择或输入的值发生改变时对自身或者其它组件产生影响称为联动.(注:editable确定是否可以手动输入) 有两种实现方法: 一.自己写对应的onChang ...
- MySQL->索引的维护[20180504]
学习MySQL数据库中表的索引维护(新增和删除) 索引的好处: 提高查询的效率 可限定特定的资料(如唯一) 索引的不足: ...
- 小白的Unity5之路(二)镜头平滑跟随角色
这次要完成Camera跟随Player移动, 首先考虑Camera的跟随目标target和平滑移动速度smothing再考虑Camera与Player的偏移量(就是Camera与Player有一个永恒 ...
- 【visual studio code 的python开发环境搭建 】
打开vs code,按按F1或者Ctrl+Shift+P打开命令行,然后输入ext install 输入Python,选第一个,这个用的最多,支持自动补全代码等功能,点击安装按钮,即可安装 下面试着编 ...
- HTML学习笔记--元素
1. 开始标签称为起始标签,结束标签称为闭合标签 openging tag closing tag HTML 元素以开始标签起始 HTML 元素以结束标签终止 元素的内容是开始标签与结束标签之间的内容 ...
- GUN交叉工具链各模块的作用
名称 归属 作用 arm-linux-as binutils 编译ARM 汇编程序 arm ...
- python3 class类 练习题
"""一.定义一个学生Student类.有下面的类属性:1 姓名 name2 年龄 age3 成绩 score(语文,数学,英语) [每课成绩的类型为整数] 类方法:1 ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...