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

简单的集合中,去重就可以用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. jmeter之JDBC Request各种数据库配置

    URL和JDBC驱动: Datebase Driver class Database URL MySQL com.mysql.jdbc.Driver jdbc:mysql://host:port/{d ...

  2. 字符串函数之Strtok()函数

    Strtok()函数详解:   该函数包含在"string.h"头文件中 函数原型: char* strtok (char* str,constchar* delimiters ) ...

  3. 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第6章编程练习2

    #include <iostream> #include <cctype> using namespace std; const int MAXSIZE=10; int mai ...

  4. VS的Mvc项目右键没有控制器右键菜单(转)

    今天遇到了一个比较少见的问题,我用vs2012打开一个从Svn上拉下来的mvc5项目,在Controller文件夹上右键却发现没有新建控制器的选项,在View文件夹上右键也没有新建视图的选项. 我的第 ...

  5. TestNG 中DataProvider 的用法

    使用DataProvider提供数据有两种形式: 第一种:一种是在测试代码和测试数据放在同一个类中: 第二种:把所有的数据提供都单独写在一个类里面,当测试数据比较多时,这种方法利于维护. DataPr ...

  6. iOS 开发中字典和字符串的转换

    1.字符串转字典 NSString * jsonString = @""; NSData *jsonData = [jsonString dataUsingEncoding:NSU ...

  7. ul的margin撑不开想要的距离的办法

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. Linux 的 date 日期的使用

    解决的问题有: 1:查看当前日期和时间 2:查看前几天的日期,或者后几天的日期. 3:查看上周几的日期,或者下周几的日期. 4:查看前后几小时,分钟.时间 编写一个简单的shell: 输出下个星期二的 ...

  9. CGI的工作原理

    文章摘自https://blog.csdn.net/nyist327/article/details/41049699 CGI是Web服务器和外部程序之间的一个接口.利用CGI程序可以处理从Web上客 ...

  10. STM32 STOP模式唤醒后的时钟

    进了STOP模式后,PLL停掉了,所以,如果开始的时钟配置,用的是PLL,那么唤醒后,需要重新配置RCC. 如果使用的是PLL,及时是用MSI作为时钟源,放大出来的,比如4M的MSI,PLL放大到48 ...