List<T> 当T为值类型的时候 去重比较简单,当T为引用类型时,一般根据业务需要,根据T的中几个属性来确定是否重复,从而去重。

查看System.Linq下的Enumerable存在一个去重方法

    /// <summary>Returns distinct elements from a sequence by using a specified <see cref="T:System.Collections.Generic.IEqualityComparer`1" /> to compare values.</summary>
/// <param name="source">The sequence to remove duplicate elements from.</param>
/// <param name="comparer">An <see cref="T:System.Collections.Generic.IEqualityComparer`1" /> to compare values.</param>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <returns>An <see cref="T:System.Collections.Generic.IEnumerable`1" /> that contains distinct elements from the source sequence.</returns>
/// <exception cref="T:System.ArgumentNullException">
/// <paramref name="source" /> is <see langword="null" />.</exception>
[__DynamicallyInvokable]
public static IEnumerable<TSource> Distinct<TSource>(this IEnumerable<TSource> source, IEqualityComparer<TSource> comparer)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
return DistinctIterator(source, comparer);
}

通过实现IEqualityComparer<T>比较器来实现对象的比较。

IEqualityComparer<T>的简单实现,通过委托来比较对象

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace Extensions
{
public delegate bool ComparerDelegate<T>(T x, T y); /// <summary>
/// list比较
/// </summary>
/// <typeparam name="T"></typeparam>
public class ListCompare<T> : IEqualityComparer<T>
{
private ComparerDelegate<T> _comparer; public ListCompare(ComparerDelegate<T> @delegate)
{
this._comparer = @delegate;
} public bool Equals(T x, T y)
{
if (ReferenceEquals(x, y))
{
return true;
}
if (_comparer != null)
{
return this._comparer(x, y);
}
else
{
return false;
}
} public int GetHashCode(T obj)
{
return obj.ToString().GetHashCode();
}
}
}

使用方法:

list= List.Distinct(new ListCompare<Path>
((x, y) => x.Latitude == y.Latitude && x.Longitude == y.Longitude)).ToList();

List去重的实现的更多相关文章

  1. JavaScript常见的五种数组去重的方式

    ▓▓▓▓▓▓ 大致介绍 JavaScript的数组去重问题在许多面试中都会遇到,现在做个总结 先来建立一个数组 var arr = [1,2,3,3,2,'我','我',34,'我的',NaN,NaN ...

  2. 数组去重 JS

    我说的数组去重是这样的: var arr = ['f', 'a',  'b', 'd', 'e', 'g']  ; var str='f'; 去除arr中的str 最简单的是遍历arr与str做比较, ...

  3. [Algorithm] 使用SimHash进行海量文本去重

    在之前的两篇博文分别介绍了常用的hash方法([Data Structure & Algorithm] Hash那点事儿)以及局部敏感hash算法([Algorithm] 局部敏感哈希算法(L ...

  4. JS去重及字符串奇数位小写转大写

    面试中经常会考到数组的去重.作为一名合格的前端开发者,不知道几种去重方法是在不应该.废话不多说直接开撸-- 一.indexOf()方法 实现思路:使用indexOf()方法来判断新数组中是否有这个值, ...

  5. js数组去重

    这就是数组去重了...var str=['hello','node','element','node','hello','blue','red'];var str1=[]; function firs ...

  6. [Hadoop]-从数据去重认识MapReduce

    这学期刚好开了一门大数据的课,就是完完全全简简单单的介绍的那种,然后就接触到这里面最被人熟知的Hadoop了.看了官网的教程[吐槽一下,果然英语还是很重要!],嗯啊,一知半解地搭建了本地和伪分布式的, ...

  7. 1.uniq去重命令讲解

    uniq命令: 常见参数: -c,--count *****      在每行旁边显示改行重复出现的次数 -d,--repeated        仅显示重复出现的行,2次或2次以上的行,默认的去重包 ...

  8. python list dict 去重的两种方式

    def dedupe(items, key=None): seen = set() for item in items: val = item if key is None else key(item ...

  9. js给数组去重写法

    数组为 var list =['A','B','A']; 法一:常规做法,新建list,给list添加元素,添加前判断是否包含 var removeRepeatItem = function(list ...

  10. 分享一种容易理解的js去重排序方法

    <script> var arr=[1,8,6,4,88,22,99,4,6,86,5,58,89,5]; //先使用sort()函数去重 var a=arr.sort(function ...

随机推荐

  1. Win10安装cygwin并添加apt-cyg

    1.去Cygwin官网:https://www.cygwin.com/ 进入上图的install链接(下图),根据自己的电脑选择32位还是64位 我选择了一个32位的: 一直下一步下图: 163镜像链 ...

  2. 分布式系统关注点(17)——先写DB还是「缓存」?

    如果第二次看到我的文章,欢迎右侧扫码订阅我哟~ 

  3. JS的 try catch使用心得

    try{ //正常执行 }catch(e/*你感觉会出错的 错误类型*/){ // 可能出现的意外 eg:用户自己操作失误 或者 函数少条件 不影响下面的函数执行 // 有时也会用在 比如 focus ...

  4. SpringBoot进阶教程(二十四)整合Redis

    缓存现在几乎是所有中大型网站都在用的必杀技,合理的利用缓存不仅能够提升网站访问速度,还能大大降低数据库的压力.Redis提供了键过期功能,也提供了灵活的键淘汰策略,所以,现在Redis用在缓存的场合非 ...

  5. win10安装gitLab

    从控制面板选择hyper-V进行安装 1.打开控制面板选择程序=>选择启用或关闭windows功能=>选择Hyper-v 安装ubuntu 1.下载ubuntu系统(本次安装为18.04. ...

  6. vscode restclient 插件

    使用步骤: 1.vscode 安装restclient 扩展 2.创建  .http 或 .rest 文件 ,编写相应内容 同一个文件内 可以通过 ### 分割多个请求 可以通过 @hostname ...

  7. Eclipse4JavaEE安装Gradle,并导入我们的Gradle项目

    第一步:下载Gradle Gradle下载链接,如下图,下载最新版本即可.下载下来的zip包,解压到一个目录即可,如F盘 第二步:配置环境变量 首先添加GRADLE_HOME,如下图 然后在Path下 ...

  8. 【代码总结● Swing中的一些操作与设置】

    Swing中设置关闭窗口验证与添加背景图片 package com.swing.test; import java.awt.EventQueue; import java.awt.Image; imp ...

  9. jQuery中对未来的元素绑定事件用 on

    最近项目需要点击弹窗里面的a标签出现外连接跳转提示 <a href="javascript:void(0);" target="_blank" id=&q ...

  10. mybatis的时间比较 xml 及不解析<=的写法

    <if test="type ==1"> and DATE_FORMAT(create_date,'%Y-%m-%d') = DATE_FORMAT(now(),'%Y ...