其他扩展方法详见:https://www.cnblogs.com/zhuanjiao/p/12060937.html

IEnumerable的Distinct扩展方法,当集合元素为对象时,可用于元素对象指定字段进行排重集

一、通过指定单个属性进行去重。

using System;
using System.Collections.Generic;
using System.Linq; namespace CoSubject.Common.CommonExtensions
{
/// <summary>
/// IEnumerable的Distinct扩展方法
/// 当集合元素为对象时,可用于元素对象指定字段进行排重集
/// </summary>
public static class DistinctExtensions
{
public static IEnumerable<T> Distinct<T, V>(this IEnumerable<T> source, Func<T, V> keySelector)
{
return source.Distinct(new CommonEqualityComparer<T, V>(keySelector));
} public static IEnumerable<T> Distinct<T, V>(this IEnumerable<T> source,
Func<T, V> keySelector, IEqualityComparer<V> comparer)
{
return source.Distinct(new CommonEqualityComparer<T, V>(keySelector, comparer));
}
} public class CommonEqualityComparer<T, V> : IEqualityComparer<T>
{
private Func<T, V> keySelector;
private IEqualityComparer<V> comparer; public CommonEqualityComparer(Func<T, V> keySelector, IEqualityComparer<V> comparer)
{
this.keySelector = keySelector;
this.comparer = comparer;
} public CommonEqualityComparer(Func<T, V> keySelector) : this(keySelector, EqualityComparer<V>.Default)
{ } public bool Equals(T x, T y)
{
return comparer.Equals(keySelector(x), keySelector(y));
} public int GetHashCode(T obj)
{
return comparer.GetHashCode(keySelector(obj));
}
}
}

  

举例:

var member = memberAll.Distinct(d => d.MemberID); // 按照MemberID进行排重,不区分大小写

var member = memberAll.Distinct(d => d.MemberID, StringComparer.CurrentCultureIgnoreCase);// 不区分大小写

两个参数的扩展方法,第二个参数有以下几种可选。

二、若是对多个属性去重如何实现呢?

思路:主要是去实现IEqualityComparer<T> 泛型接口中的两个方法,Equals和GetHashCode,根据自己的需求去返回真假

具体实现参照https://www.zhangshengrong.com/p/JKN8Eqo2X6/

因为对象在比较的时候,会先调用GetHashCode方法,

若HashCode不同 ,则对象不同,不会调用Equlas方法,

若HashCode相同,再调用Equlas方法进行比较

文章里面就是: 让GetHashCode方法返回常量,触发Equlas方法进行比较,Equlas里面写了自己所需要排重的属性进行判断

三、排重是否有其他方式可以实现?

有,memberAll.Where((m,i)=>memberAll.FindIndex(z=>z.MemberID== m.MemberID) == i)

另,GroupBy 可以实现

C# 扩展方法——去重(Distinct)的更多相关文章

  1. .NET-list扩展方法Distinct去重

    原文链接:https://blog.csdn.net/daigualu/article/details/70800012 .NET中list的扩展方法Distinct可以去掉重复的元素,分别总结默认去 ...

  2. 【C#】详解使用Enumerable.Distinct方法去重

    Enumerable.Distinct 方法 是常用的LINQ扩展方法,属于System.Linq的Enumerable方法,可用于去除数组.集合中的重复元素,还可以自定义去重的规则. 有两个重载方法 ...

  3. Linq Enumerable.Distinct方法去重

    Enumerable.Distinct 方法 是常用的LINQ扩展方法,属于System.Linq的Enumerable方法,可用于去除数组.集合中的重复元素,还可以自定义去重的规则. 有两个重载方法 ...

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

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

  5. Hive中笔记 :三种去重方法,distinct,group by与ROW_Number()窗口函数

    一.distinct,group by与ROW_Number()窗口函数使用方法 1. Distinct用法:对select 后面所有字段去重,并不能只对一列去重. (1)当distinct应用到多个 ...

  6. 扩展Linq的Distinct方法动态根据条件进行筛选

    声明为了方便自己查看所以引用 原文地址:http://www.cnblogs.com/A_ming/archive/2013/05/24/3097062.html Person1: Id=1, Nam ...

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

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

  8. .NET 实用扩展方法

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

  9. 数据去重Distinct,IEqualityComparer,IEquatable

    很多情况下我们查询数据需要去重重复数据,下面就记录三个去重的方法. Distinct 最基本的去重形式,直接查询出数据后使用Distinct方法进行字段去重. var strList = new Li ...

随机推荐

  1. w 命令

    NAME w - Show who is logged on and what they are doing. SYNOPSIS w - [husfiV] [user] 参数说明: -f 开启或关闭显 ...

  2. 【Linux-驱动】将cdev加入到系统中去---cdev_add

    在我们已经完成了对cdev结构体的初始化之后,我们需要将这个cdev结构体加入到系统中去,使用函数 cdev_add: /** * cdev_add() 讲一个字符设备加入到系统中去 * @p: 字符 ...

  3. [转帖]yum命令的使用与createrepo自建仓库教程

    yum命令的使用与createrepo自建仓库教程 http://www.linuxe.cn/post-300.html 跟上篇一样 可以学习一下. 发布:TangLu2018-11-23 16:48 ...

  4. 查找担保圈-step3-获取担保圈路径

    USE [test] GO /****** Object: StoredProcedure [dbo].[p01_get_group_path] Script Date: 2019/7/8 14:40 ...

  5. 阿里云服务器挖矿脚本bioset攻击解决

    1.问题出现 一大早刚起床,阿里云就给我发了一条短信,提醒我服务器出现紧急安全事件:挖矿程序 阿里云“贴心”地提供了解决方法,不过需要购买企业版的安全服务,本着能自己动手就不花钱原则自己搞了起来 于是 ...

  6. springboot 用redis缓存整合spring cache注解,使用Json序列化和反序列化。

    springboot下用cache注解整合redis并使用json序列化反序列化. cache注解整合redis 最近发现spring的注解用起来真的是很方便.随即产生了能不能吧spring注解使用r ...

  7. Luogu P5068 [Ynoi2015]我回来了

    题目 Ynoi难得的水题. 首先我们可以\(O(n^2)\)地求出任意两点之间的距离. 然后我们可以\(O(n^3)\)地求出对于任意一个点\(u\),跟它距离\(\le d\)的点的集合. 然后对于 ...

  8. 前端之BOM,DOM

    前戏 到目前为止,我们已经学过了JavaScript的一些简单的语法.但是这些简单的语法,并没有和浏览器有任何交互. 也就是我们还不能制作一些我们经常看到的网页的一些交互,我们需要继续学习BOM和DO ...

  9. shell脚本之nginx启动脚本、统计日志字段、for循环实战、跳板机

    1.NGINX启动脚本 #!/bin/bash # chkconfig: 235 32 62 # description: nginx [ -f /etc/init.d/functions ] &am ...

  10. Collection<E>接口

    https://docs.oracle.com/javase/8/docs/api/java/util/Collection.html public interface Collection<E ...