原文链接:https://blog.csdn.net/daigualu/article/details/70800012

.NET中list的扩展方法Distinct可以去掉重复的元素,分别总结默认去重和自定义去重。

 class Program
{
static void Main(string[] args)
{
//Distinct去重 //默认去重
List<int> list1 = new List<int> {, , , , };
List<int> list2 = list1.Distinct().ToList(); //出去重复元素,列表变为{3,9,2} //自定义去重
List<Test> list3 = new List<Test>();
list3.Add(new Test(){Data=});
list3.Add(new Test() { Data = });
list3.Add(new Test() { Data = });
list3.Add(new Test() { Data = });
list3.Add(new Test() { Data = });
List<Test> list4 = list3.Distinct(new TestDuplicateDefine()).ToList();//变为3项
} public class Test
{
public int Data { get; set; }
} public class TestDuplicateDefine: IEqualityComparer<Test>
{
public bool Equals(Test x, Test y)
{
return x.Data == y.Data;
} public int GetHashCode(Test obj)
{
return obj.ToString().GetHashCode();
}
} }

.NET-list扩展方法Distinct去重的更多相关文章

  1. C# 扩展方法——去重(Distinct)

    其他扩展方法详见:https://www.cnblogs.com/zhuanjiao/p/12060937.html IEnumerable的Distinct扩展方法,当集合元素为对象时,可用于元素对 ...

  2. c# 扩展方法奇思妙用基础篇八:Distinct 扩展(转载)

    转载地址:http://www.cnblogs.com/ldp615/archive/2011/08/01/distinct-entension.html 刚看了篇文章 <Linq的Distin ...

  3. c# 扩展方法奇思妙用基础篇八:Distinct 扩展

    刚看了篇文章 <Linq的Distinct太不给力了>,文中给出了一个解决办法,略显复杂. 试想如果能写成下面的样子,是不是更简单优雅 var p1 = products.Distinct ...

  4. .NET 实用扩展方法

    .NET 实用扩展方法(持续更新...) 1. 字符串转换为可空数值类型(int, long, float...类似) /// <summary> /// 将字符串转换成32位整数,转换失 ...

  5. 【EF学习笔记11】----------查询中常用的扩展方法

    先来看一下我们的表结构: 首先毫无疑问的要创建我们的上下文对象: using (var db = new Entities()) { //执行操作 } Average 平均值: //查询平均分 Con ...

  6. c# 扩展方法奇思妙用

    # 扩展方法出来已久,介绍扩展方法的文章也很多,但都是笼统的.本人最近一直在思考扩展方法的应用,也悟出了一些,准备将这最近一段时间对扩展方法的思考,写成一个系列文章.每个文章只介绍一个应用方面,篇幅不 ...

  7. 从扩展方法到匿名方法再到LINQ

    1.首先我们应该知道什么是扩展方法: 扩展方法使您能够向现有类型“添加”方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样 ...

  8. ASP.Net string 类的扩展方法 [转]

    string 类的扩展方法列表(基本相同于 IEnumerable<T> 接口的成员列表): Aggregate<>     //累加 All<>        / ...

  9. C# LiNq的语法以及常用的扩展方法

    首先先来扯一下,这篇博文是我第一次写的,主要是我的一些摘录,希望对大家有所帮助. Linq的基础 •LINQ(读音link):Linq To SQL(过时).Linq To Object.Linq T ...

随机推荐

  1. 【Python开发】python PIL读取图像转换为灰度图及另存为其它格式(也可批量改格式)

    例如有一幅图,文件名为"a.jpg'.  读取: from PIL import Image #或直接import Image im = Image.open('a.jpg') 将图片转换成 ...

  2. PTA(Advanced Level)1031.Hello World for U

    Given any string of N (≥5) characters, you are asked to form the characters into the shape of U. For ...

  3. PostgreSQL查看等待锁的SQL和进程

    查看等待锁的查询和进程: The following query may be helpful to see what processes are blocking SQL statements (t ...

  4. redis缓存穿透00

    缓存穿透 缓存穿透,是指查询一个数据库一定不存在的数据.正常的使用缓存流程大致是,数据查询先进行缓存查询,如果key不存在或者key已经过期,再对数据库进行查询,并把查询到的对象,放进缓存.如果数据库 ...

  5. Head First PHP&MySQl第一章代码

    HTML: <!doctype html> <html lang="zh-cn"> <head> <meta charset=" ...

  6. Python验证码登录(Tesseract安装配置)

    1.安装py库:pytesseract,PIL pip install pytesseract pip install PILLOW 如果安装时,出现权限不足: pip install --user ...

  7. 快速幂(Fast_Power)

    定义快速幂顾名思义,就是快速算某个数的多少次幂. 其时间复杂度为 O(log2N), 与朴素的O(N)相比效率有了极大的提高. 以下以求a的b次方来介绍 原理把b转换成2进制数 该2进制数第i位的权为 ...

  8. springboot拦截中自动注入的组件为null问题解决方法

    一.写SpringUtil类来获取Springh管理的类实例,判断是否注入成功,如果没有注入成功重新获取注入 package com.util; import org.springframework. ...

  9. gitlab操作笔记

    基本命令 准备 1. 安装所需命令 sudo yum install curl openssh-server openssh-clients postfix cronie -y2. 安装SSH sud ...

  10. C数据结构排序算法——希尔排序法用法总结(转http://www.cnblogs.com/skywang12345/p/3597597.html)

    希尔排序介绍 希尔排序(Shell Sort)是插入排序的一种,它是针对直接插入排序算法的改进.该方法又称缩小增量排序,因DL.Shell于1959年提出而得名. 希尔排序实质上是一种分组插入方法.它 ...