由于业务场景的需要,海量的数据需要进行处理、组装,难免会出现冗余的重复数据。如何处理重复的数据就是一个问题。

简单的集合中,去重就可以用linq distinct来完成。对于复杂的集合直接使用distinct就会显得没那么有效了。

  • 造数据

构造1M的orderentity,非重复的数据为1M/2.

 IList<OrderEntity> sourceList = new List<OrderEntity>();
for (int i = ; i < ; i++)
{
OrderEntity o = new OrderEntity
{
OrderNo = i % ,
Amount = ,
Detail = "test"
};
sourceList.Add(o);
}

方式一:直接distinct

 var list = sourceList.Distinct().ToList();
Console.WriteLine(list.Count + " 耗时:" + watch.ElapsedMilliseconds);

结果还是1M,对于复杂的集合 distinct直接使用是没效果的。

方法二:对数据分组

 var list2 = sourceList.GroupBy(t => new
{
t.OrderNo,
t.Amount,
t.Detail }).Select(g => g.First()).ToList(); Console.WriteLine(list2.Count + " 耗时:" + watch.ElapsedMilliseconds);

结果是500K, 对集合group处理还是有作用的,可惜的是耗时较高。

方法三:推荐 使用Distinct 重载

 public class OrderEntityComparer : IEqualityComparer<OrderEntity>
{
public bool Equals(OrderEntity x, OrderEntity y)
{
if (Object.ReferenceEquals(x, y)) return true;
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
return x.OrderNo == y.OrderNo && x.Amount == x.Amount && x.Detail == y.Detail;
} public int GetHashCode(OrderEntity obj)
{
//Check whether the object is null
if (Object.ReferenceEquals(obj, null)) return ;
//Get hash code for the Name field if it is not null.
int hashOrderNo = obj.OrderNo.GetHashCode(); //Get hash code for the Code field.
int hashAmount = obj.Amount.GetHashCode(); int hashDetail = obj.Detail == null ? : obj.Detail.GetHashCode();
//Calculate the hash code for the product.
return hashOrderNo ^ hashAmount ^ hashDetail;
}
}
  var list3 = sourceList.Distinct(new OrderEntityComparer()).ToList();

 Console.WriteLine(list3.Count + " 耗时:" + watch.ElapsedMilliseconds);

结果:达到去重目的,耗时也可以接受。

.Net Collection Distinct 去重的更多相关文章

  1. Linq 中的distinct去重

    Linq的Distinct和T-Sql的distinct一样,可以将重复的结果集去重注意: 1 distinct去重记录要求每个字段都重复时,才算重复对象,这与sql一样2 distinct语句可以和 ...

  2. 存储过程系列三:根据表别名方式distinct去重插入

    1.根据表别名方式distinct去重插入 insert into GG_XKZ_YLQXSCXKESL_SCDZ           ( bzj, xkzid,  sqid, jtdz, szsf, ...

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

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

  4. postgresql中使用distinct去重

    select语法 [ WITH [ RECURSIVE ] with_query [, ...] ] SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ...

  5. List<object>进行Distinct()去重

    有时我们会对一个list<T>集合里的数据进行去重,C#提供了一个Distinct()方法直接可以点得出来.如果list<T>中的T是个自定义对象时直接对集合Distinct是 ...

  6. DISTINCT 去重仍有重复的分析

    logger日志报错 插入数据时违反主键唯一约束 org.springframework.dao.DuplicateKeyException: ### Error updating database. ...

  7. C# Distinct去重泛型List

    List<int>去重 List<string>去重 List<T>去重 1. List<int>去重 List<int> ilist = ...

  8. 关于Django中的数据库操作API之distinct去重的一个误传

    转载自http://www.360doc.com/content/18/0731/18/58287567_774731201.shtml django提供的数据库操作API中的distinct()函数 ...

  9. SQLSERVER去除某一列的重复值并显示所有数据\DISTINCT去重\ISNULL()求SUM()\NOT EXISTS的使用

    进入正题,准备我们的测试数据 1.我们要筛选的数据为去除 GX 列的重复项 并将所有数据展示出来,如图所示: ' 2.这种情况下我们是不可以使用DISTINCT来去重的,我们可以来尝试一下: 首先,单 ...

随机推荐

  1. nodejs+koa在header里面添加header信息

    参考:https://koa.bootcss.com/ ctx.append('resultCode', '0000'); ctx.append('resultMessage', 'success') ...

  2. winform 利用Http向服务器上传与下载文件

    利用在服务器端的IIS,布置“请求处理映射”.从而处理,本地发出Post请求.Url指向web网站所在路径的请求映射.由映射代码实现服务器保存文件. winform里面使用,WebClient的对象, ...

  3. 2018-2019-1 20189210 《LInux内核原理与分析》第八周作业

    一 .可执行程序工作原理 程序编译 预处理:gcc -E hello.c -o hello.i 编译:gcc -S hello.i -o hello.s -m32 汇编:gcc -c hello.s ...

  4. Servlet实践--HelloWorld

    Servlet规范是一套技术标准,包含与Web应用相关的一系列接口,而具体的Servlet容器负责提供标准的实现,如Tomcat. Servlet的实例对象由Servlet容器负责创建,Servlet ...

  5. 网络流 ek

    hdu3549 求最大流果题 ek算法 先bfs出一条流 然后通过不断地添加增广路 得到最大流(证明在算法书上都有) 增加了一个流 就加反向边 允许程序通过走方向边的方式进行“回滚” i^1 = i+ ...

  6. es7 async/await使用

    先创建一个promise对象,里面执行一个异步函数 function fetchUser() { return new Promise((resolve, reject) => { fetch( ...

  7. Install sublime text for elementary os

    1. download sublime_text_3_build_3176_x86.tar.gz from http://www.sublimetext.com/3 2. extract it to ...

  8. JS数组reduce()方法详解及高级技巧

    1.语法 arr.reduce(callback,[initialValue]) reduce 为数组中的每一个元素依次执行回调函数,不包括数组中被删除或从未被赋值的元素,接受四个参数:初始值(或者上 ...

  9. type显示的是访问类型,是较为重要的一个指标,结果值从好到坏依次是: system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL ,一般来说,得保证查询至少达到range级别,最好能达到ref。 作者:高

    MySQL EXPLAIN详解 - 简书 https://www.jianshu.com/p/ea3fc71fdc45 type显示的是访问类型,是较为重要的一个指标,结果值从好到坏依次是: syst ...

  10. X86-32位架构的CPU是不是内存只能到4G

    不是的,可以通过分页机制扩展实现超过4G内存的支持. 什么是分页机制扩展? PAE. 什么是PAE?   PAE如何实现的?