C# List Distinct用法
class ListDistinctDemo
{
static void Main(string[] args)
{
List personList = new List(){
new Person(3),//重复数据
new Person(3),
new Person(2),
new Person(1)
};
//使用匿名方法
List delegateList = personList.Distinct(new Compare(
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 lambdaList = personList.Distinct(new Compare(
(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 x, T y);
public class Compare : IEqualityComparer
{
private EqualsComparer _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);elsereturn false;
}
public int GetHashCode(T obj)
{return obj.ToString().GetHashCode();
}
}
C# List Distinct用法相关推荐
- mysql中去重的用法_mysql中去重 distinct 用法
在使用MySQL时,有时需要查询出某个字段不重复的记录,这时可以使用mysql提供的distinct这个关键字来过滤重复的记录,但是实际中我们往往用distinct来返回不重复字段的条数(count( ...
- php distinct 用法,distinct的用法
1. Distinct 位置 单独的distinct只能放在开头,否则报错,语法错误 例:SELECT Sid,DISTINCT(Sscore) score from t_student; [SQL ...
- 表之sql删除重复数据、查找去除重复数据、DISTINCT用法
目录 一.查找去除重复数据 二.删除重复数据 三.DISTINCT用法 一.查找去除重复数据 数据如图: id可以理解为uuid,person_sort_code可以理解为唯一的标识,但是唯一标识重复 ...
- mysql distinct效率优化_mysql distinct 用法详解及优化
本事例实验用表task,结构如下 MySQL> desc task; +-------------+------------+------+-----+-------------------+- ...
- tp 中distinct用法
DISTINCT 方法用于返回唯一不同的值 . 例如: $Model->distinct(true)->field('name')->select(); 生成的SQL语句是: SEL ...
- distinct用法及其优化
在使用MySQL时,有时需要查询出某个字段不重复的记录,这时可以使用mysql提供的distinct这个关键字来过滤重复的记录,但是实际中我们往往用distinct来返回不重复字段的条数(count( ...
- mysql php distinct_mysql distinct用法是什么
mysql中去重 distinct 用法 在使用mysql时,有时需要查询出某个字段不重复的记录,这时可以使用mysql提供的distinct这个关键字来过滤重复的记录,但是实际中我们往往用disti ...
- mysql筛选不重复数据库_mysql的distinct用法-mysql中如何筛选出非重复的数据
使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供有distinct这个关键字来过滤掉多余的重复记录只保留一 条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的所 ...
- mysql级联distinct_mysql的distinct用法
使用mysql时,有时需要查询出某个字段不重复的记录,虽然mysql提供有 distinct这个关键字来过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的所 ...
- MySQL拾遗之distinct用法
目录 阅读源 阅读源 记不住的 MySQL DISTINCT 的用法 ( 上 ) https://www.twle.cn/c/yufei/mysqlfav/mysqlfav-basic-distinc ...
最新文章
- onkeypress属性添加报错_亚马逊运营过程中listing常见报错及应对策略汇总
- CentOS探索之路2---使用rpm安装JDK
- PyCharm 快捷操作
- 本地传送命令到远程主机上执行的方法
- EOJ_1039_最长连续公共子序列
- java面向对象之父类的引用指向子类的对象
- LeetCode 173. 二叉搜索树迭代器(中序遍历)
- mysql怎么保证热点数据_MySQL里有2000w数据,redis中只存20w数据,如何保证redis中数据都是热点数据...
- 【PMP认证考试之个人总结】第 3 章 项目整合管理
- 将Nginx加入service服务中
- 总结帖:“深度解析:清理烂代码”
- 简单的限流保护电路图大全(六款简单的限流保护电路设计原理图详解)
- 《博德之门3》的许多设计,还不如上世纪的前作
- WXPY自动转发群消息
- 《Python数据可视化之Matplotlib与Pyecharts》之K线图
- python杨辉三角函数_python杨辉三角输出指定行_使用python打印十行杨辉三角过程详解...
- 说说 隋唐演义 等英雄排名
- 数据库中char和nchar的区别
- [CF765F] Souvenirs
- hadoop 集群启动 ERROR: Cannot write datanode pid /tmp/hadoop-user-datanode.pid. 问题的解决