.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来去重的,我们可以来尝试一下: 首先,单 ...
随机推荐
- Hadoop Yarn 框架原理及运作机制及与MapReduce比较
Hadoop 和 MRv1 简单介绍 Hadoop 集群可从单一节点(其中所有 Hadoop 实体都在同一个节点上运行)扩展到数千个节点(其中的功能分散在各个节点之间,以增加并行处理活动).图 1 演 ...
- XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Korea
A. Donut 扫描线+线段树. #include<cstdio> #include<algorithm> using namespace std; typedef long ...
- [POJ2054]Color a Tree (并查集+贪心)
POJ终于修好啦 题意 和UVA1205是同一题,在洛谷上是紫题 有一棵树,需要给其所有节点染色,每个点染色所需的时间是一样的都是11.给每个点染色,还有一个开销“当前时间×ci×ci”,cici是每 ...
- url 中文及特殊字符转码
#include <ctype.h> std::string UrlEncode(const std::string& szToEncode) { std::string s ...
- Chrome_断点调试(使用vue-resource进行数据交互)
一.使用vue-resource插件进行数据交互式,返回的并不是直接的json数据,其实还封装了一层. 如下代码:直接使用 res.result.list 取不到数据. methods:{ cartv ...
- 编写程序,将来自文件中的行保存在一个vector<string>,然后使用一个istringstream 从vector中读取数据,每次读一个单词
#include<fstream> #include <vector> #include<string> #include<iostream> #inc ...
- [LeetCode] Most Profit Assigning Work 安排最大利润的工作
We have jobs: difficulty[i] is the difficulty of the ith job, and profit[i] is the profit of the ith ...
- jquery各版本
各版本下载: https://www.jb51.net/zt/jquerydown.htm
- oralce 将浮点型字段,转为指定精度 并且四舍五入
使用 Round() 函数,该函数有两个参数,第一个参数: 将要转换的字段,第二个参数:保留的小数点位数 1 代表保留1位 2 代表保留2位 例子: SELECT round(123.446,1) ...
- git开发过程中的使用流程
001.创建仓库 002.新建项目 003.初始化仓库 这一步不需要做 git init : 文件夹中会多出一个隐藏的.git文件 004.克隆项目 git clone <项目地址> 0 ...