.Net Collection Distinct 去重
由于业务场景的需要,海量的数据需要进行处理、组装,难免会出现冗余的重复数据。如何处理重复的数据就是一个问题。
简单的集合中,去重就可以用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 去重的更多相关文章
- Linq 中的distinct去重
Linq的Distinct和T-Sql的distinct一样,可以将重复的结果集去重注意: 1 distinct去重记录要求每个字段都重复时,才算重复对象,这与sql一样2 distinct语句可以和 ...
- 存储过程系列三:根据表别名方式distinct去重插入
1.根据表别名方式distinct去重插入 insert into GG_XKZ_YLQXSCXKESL_SCDZ ( bzj, xkzid, sqid, jtdz, szsf, ...
- .NET-list扩展方法Distinct去重
原文链接:https://blog.csdn.net/daigualu/article/details/70800012 .NET中list的扩展方法Distinct可以去掉重复的元素,分别总结默认去 ...
- postgresql中使用distinct去重
select语法 [ WITH [ RECURSIVE ] with_query [, ...] ] SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ...
- List<object>进行Distinct()去重
有时我们会对一个list<T>集合里的数据进行去重,C#提供了一个Distinct()方法直接可以点得出来.如果list<T>中的T是个自定义对象时直接对集合Distinct是 ...
- DISTINCT 去重仍有重复的分析
logger日志报错 插入数据时违反主键唯一约束 org.springframework.dao.DuplicateKeyException: ### Error updating database. ...
- C# Distinct去重泛型List
List<int>去重 List<string>去重 List<T>去重 1. List<int>去重 List<int> ilist = ...
- 关于Django中的数据库操作API之distinct去重的一个误传
转载自http://www.360doc.com/content/18/0731/18/58287567_774731201.shtml django提供的数据库操作API中的distinct()函数 ...
- SQLSERVER去除某一列的重复值并显示所有数据\DISTINCT去重\ISNULL()求SUM()\NOT EXISTS的使用
进入正题,准备我们的测试数据 1.我们要筛选的数据为去除 GX 列的重复项 并将所有数据展示出来,如图所示: ' 2.这种情况下我们是不可以使用DISTINCT来去重的,我们可以来尝试一下: 首先,单 ...
随机推荐
- Visual GC提示"不受此JVM支持“解决方案(配置jstatd)
Visual GC提示"不受此JVM支持“,如果想使用这个插件,就需要配置jstatd连接方式,下面来看jstatd的配置: 1.配置安全策略文件路径$JAVA_HOME/jre/lib/s ...
- [数据结构] 快速排序C语言程序
//由大到小//快速排序(待排序数组,左侧起点,右侧起点) void quickSort(int *array, int l, int r) { if ( l >= r) return; int ...
- 版本管理_svn_检出_更新_提交_本机 MAC 地址_冲突
版本管理 git svn [subversion] 实现集中式的版本控制工具软件 来源于最初的工程图纸. 需求: 备份还原 协同修改 权限控制 弊端:集中式(管理方便,但是如果中心服务器瘫痪,整个开发 ...
- ECMA Script 6_async 函数
async 函数 const promise = new Promise((resolve, reject)=>{ setTimeout(function(){ console.log(&quo ...
- [LeetCode] Linked List Components 链表组件
We are given head, the head node of a linked list containing unique integer values. We are also give ...
- CentOS系统下安装python3+Django
转载:CentOS系统下安装python3+Django 1.首先用yum安装下vim,因为CentOS 7可能根本没自带完整vim,经常出现输入乱码:yum -y install vim 2.安装开 ...
- radio选中与不选中
$("input[name='bankCardType']").val("sdf"); $('input:radio[name="bankCardTy ...
- C#获取本周五日期字符串
using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using ...
- js除法四舍五入
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- vue设置背景图片
现在data里面定义: note: { backgroundImage: "url(" + require("../../assets/home/bigdatabak.p ...