前言

问题描述:Person类有两个属性ID(int)、Name(string)属性。筛选序列中不重复的Person。

   1: public class Person
   2: {
   3:     public int ID { get; set; }
   4:     public string Name { get; set; }
   5: }

好吧,看样子得用Distinct方法。不过Distinct方法有两个重载。

   1: public static IEnumerable<TSource> Distinct<TSource>(
   2:     this IEnumerable<TSource> source
   3: )
   4:  
   5: public static IEnumerable<TSource> Distinct<TSource>(
   6:     this IEnumerable<TSource> source,
   7:     IEqualityComparer<TSource> comparer
   8: )

看样子已经够用了。不过如果我想只按ID唯一,或者按Name唯一呢?派生IEqualityComparer倒是可以解决。不过那也忒坑爹了。还是自己试着写一个扩展吧。

Distinct扩展

版本1

参考封装的Distinct的方法先写一个。声明一个List<TSource>,循环的时候比较,如果不同就加进去。

   1: public static IEnumerable<TSource> Distinct<TSource, TProperty>(this IEnumerable<TSource> source, Func<TSource, TProperty> selector)
   2: {
   3:     if (source == null) throw new ArgumentNullException("source");
   4:     if (selector == null) throw new ArgumentNullException("selector");
   5:     List<TSource> temp = new List<TSource>();
   6:     foreach (var item in source)
   7:     {
   8:         if (temp.Any(t => selector(t).Equals(selector(item))))
   9:         {
  10:             continue;
  11:         }
  12:         temp.Add(item);
  13:     }
  14:     return temp;
  15: }

测试一下,没什么问题。不过在函数里声明List<TSource>感觉怪怪的,想办法换种方式。

   1: List<Person> list = new List<Person>() 
   2: {
   3:     new Person() { ID = 1, Name = "Alen1" },
   4:     new Person() { ID = 1, Name = "Alen2" },
   5:     new Person() { ID = 2, Name = "Alen2" },
   6:     new Person() { ID = 2, Name = "Alen1" }
   7: };
   8:  
   9: var temp = list.Distinct(t => t.ID);

版本2

取指定属性的唯一序列,在循环的时候使用yield返回。

   1: public static IEnumerable<TSource> Distinct<TSource, TProperty>(this IEnumerable<TSource> source, Func<TSource, TProperty> selector)
   2: {
   3:     if (source == null) throw new ArgumentNullException("source");
   4:     if (selector == null) throw new ArgumentNullException("selector");
   5:  
   6:     var pdis = source.Select(t => selector(t)).Distinct();
   7:     foreach (var item in pdis)
   8:     {
   9:         yield return source.First(t => selector(t).Equals(item));
  10:     }
  11: }

测试通过。

用Stopwatch分别测试两个函数循环不同次数的时间。对比一下,循环次数在10,000次以上时,版本2耗时大概是版本1的1/40,性能差距算是比较大。

后记

目前来说已经够用了,如果还有其他需求,再根据情况扩展吧。

   1: list.Distinct(t => t.ID);
   2: list.Distinct(t => new { t.ID, t.Name }); //根据ID及Name的值分别比较来取唯一,并非比较引用

转载于:https://www.cnblogs.com/ainijiutian/archive/2012/12/12/2815007.html

Linq Distinct扩展相关推荐

  1. c# 扩展方法奇思妙用基础篇八:Distinct 扩展(转载)

    转载地址:http://www.cnblogs.com/ldp615/archive/2011/08/01/distinct-entension.html 刚看了篇文章 <Linq的Distin ...

  2. linq distinct 不够用了!

    问题引出:在实际中遇到一个问题,要进行集合去重,集合内存储的是引用类型,需要根据id进行去重.这个时候linq 的distinct 就不够用了,对于引用类型,它直接比较地址.测试数据如下: class ...

  3. Linq distinct去重方法之一

    var result = query.Distinct().ToList(); List<DeliveryOrderViewModel> dov = result.GroupBy( p = ...

  4. dapper mysql 拓展_Dapper.Common基于Dapper的开源LINQ超轻量扩展

    Dapper.Common Dapper.Common是基于Dapper的LINQ实现,支持.net core,遵循Linq语法规则.链式调用.配置简单.上手快,支持Mysql,Sqlserver(目 ...

  5. Dapper.Common基于Dapper的开源LINQ超轻量扩展

    Dapper.Common Dapper.Common是基于Dapper的LINQ实现,支持.net core,遵循Linq语法规则.链式调用.配置简单.上手快,支持Mysql,Sqlserver(目 ...

  6. linq Distinct

    private static void D1()         {             int[] ints = new int[] { 1, 1, 11, 3, 2, 4, 5, 6, 7, ...

  7. Linq的Distinct方法的扩展

    为什么80%的码农都做不了架构师?>>>    原文地址:如何很好的使用Linq的Distinct方法 Person1: Id=1, Name="Test1" P ...

  8. lambda 根据属性去重_扩展lamda表达中distinct按照字段去除重复

    首先,我们定义一个Student类来测试. public classStudent {public int ID { get; set; }public string Name { get; set; ...

  9. 如何很好的使用Linq的Distinct方法

    Person1: Id=1, Name="Test1" Person2: Id=1, Name="Test1" Person3: Id=2, Name=&quo ...

最新文章

  1. Java主方法引用传递_java方法中的参数传递是值传递还是引用传递(转)
  2. JPA连接Mysql数据库时提示:Table 'jpa.sequence' dosen't exisit
  3. 【工业控制】PolyWorks培训教程-PCB字符机平行度和垂直度
  4. OpenGL 学习笔记(3)绘制几何物体
  5. SAP Cloud for Customer Cloud Application Studio的Trace功能
  6. 英国萨里大学金耀初教授:进化计算在人工智能领域的发展
  7. HH SaaS电商系统的标签系统设计
  8. Educational Codeforces Round 1(D. Igor In the Museum) (BFS+离线访问)
  9. 【hortonworks/registry】AVRO 规范-Schema的定义和声明
  10. OpenShift 4 - 使用Debezium实现MySQL的CDC变化数据捕获
  11. ERP学习 之 财务管理
  12. 系统管理员已经限制你可以使用的登录类型(网络或交互式)
  13. linux删除ip地址的命令
  14. idea选中多行的一列、一竖(不是多行的全部内容)
  15. C语言循环之空心梯形,循环-空心梯形
  16. 下载的中文文件名乱码,如何转码
  17. 搭建私服环境及私服的使用-将第三方jar上传私服
  18. cocos2dx的图片加载
  19. 5分钟学会Python爬取整个网站
  20. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord)

热门文章

  1. hello python jpush api_jpush python服务器端
  2. babel import语法 js_搭建开发JS库的运行环境
  3. 如何查看抓包文件所使用的捕获过滤器
  4. Xamarin XAML语言教程构建进度条ProgressBar
  5. Xamarin iOS开发实战上册(内部资料daxueba.net)
  6. java强制转换成float_在Java中什么时候double必须强制转换成float 就是要在数值后加f或者强制转换...
  7. jira 审批流程_博兴县行政审批服务局推暖心服务工程 企业开办实现“全程网办”_博兴新闻...
  8. html中加载shp文件,运用shapefile.js解析Shp文件
  9. 你知道吗?脑机接口训练会对大脑物质结构和功能产生影响
  10. tf.metrics.accuracy