引自:http://blog.csdn.net/shaopengfei/article/details/36426763

从C# 3.0开始提供了Distinct方法,这对于集合的使用有了更为丰富的方法,经过在网上搜索相应的资源,发现有关这方面的写的好的文章还是不少的。而且为了扩展Linq的使用不方便的地方,有一些办法非常有效。由于本人工作中的需要,有一些功能暂时没有用到那么深入,现在只把最简单的一些功能分享出来,整理出来。

  1. 简单一维集合的使用:

    1. List<int> ages = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 };
    2. List<string> names = new List<string> { "wang", "li", "zhang", "li", "wang", "chen", "he", "wang" };
    3. IEnumerable<int> distinctAges = ages.Distinct();
    4. Console.WriteLine("Distinct ages:");
    5. foreach (int age in distinctAges)
    6. {
    7. Console.WriteLine(age);
    8. }
    9. var distinctNames = names.Distinct();
    10. Console.WriteLine("\nDistinct names:");
    11. foreach (string name in distinctNames)
    12. {
    13. Console.WriteLine(name);
    14. }
    • 在这段代码中,是最简单的Distinct()方法的使用。使用了集合接口IEnumerable,以及隐式类型var,至于这两种用法有什么区别,没有研究出来。
    • 但是如果象下面这样的代码,是错误的!
    1. List<int> disAge = ages.Distinct();
    • 正确的方法应该是:
    1. List<int> ages = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 };
    2. List<int> disAge = ages.Distinct().ToList();
    3. foreach (int a in disAge)
    4. Console.WriteLine(a);
    • 也就是说Distinct()方法的返回集合类型是一个接口,不是具体的集合,所以需要用一个ToList()。
  2. 自定义类的使用:

    • 首先我们看MSDN上给出的例子,先定义一个产品类:
    1. public class Product : IEquatable<Product>
    2. {
    3. public string Name { get; set; }
    4. public int Code { get; set; }
    5. public bool Equals(Product other)
    6. {
    7. //Check whether the compared object is null.
    8. if (Object.ReferenceEquals(other, null)) return false;
    9. //Check whether the compared object references the same data.
    10. if (Object.ReferenceEquals(this, other)) return true;
    11. //Check whether the products' properties are equal.
    12. return Code.Equals(other.Code) && Name.Equals(other.Name);
    13. }
    14. // If Equals() returns true for a pair of objects
    15. // then GetHashCode() must return the same value for these objects.
    16. public override int GetHashCode()
    17. {
    18. //Get hash code for the Name field if it is not null.
    19. int hashProductName = Name == null ? 0 : Name.GetHashCode();
    20. //Get hash code for the Code field.
    21. int hashProductCode = Code.GetHashCode();
    22. //Calculate the hash code for the product.
    23. return hashProductName ^ hashProductCode;
    24. }
    25. }
    • 在主函数里,是这样用的:
    1. static void Main(string[] args)
    2. {
    3. Product[] products =
    4. {
    5. new Product { Name = "apple", Code = 9 },
    6. new Product { Name = "orange", Code = 4 },
    7. new Product { Name = "apple", Code = 9 },
    8. new Product { Name = "lemon", Code = 12 }
    9. };
    10. //Exclude duplicates.
    11. IEnumerable<Product> noduplicates =
    12. products.Distinct();
    13. foreach (var product in noduplicates)
    14. Console.WriteLine(product.Name + " " + product.Code);
    15. }
    • 这样的输出是:
    1. /*
    2. This code produces the following output:
    3. apple 9
    4. orange 4
    5. lemon 12
    6. */
    • 但是现在的问题是,如果我们把主函数里改成这样:
    1. static void Main(string[] args)
    2. {
    3. Product[] products =
    4. {
    5. new Product { Name = "Smallapple", Code = 9 },
    6. new Product { Name = "orange", Code = 4 },
    7. new Product { Name = "Bigapple", Code = 9 },
    8. new Product { Name = "lemon", Code = 12 }
    9. };
    10. //Exclude duplicates.
    11. IEnumerable<Product> noduplicates =
    12. products.Distinct();
    13. foreach (var product in noduplicates)
    14. Console.WriteLine(product.Name + " " + product.Code);
    15. }
    • 这样的输出是:
    1. /*
    2. This code produces the following output:
    3. Smallapple 9
    4. orange 4
    5. Bigapple 9
    6. lemon 12
    7. */
    • 我们的问题是,如果想按Code来索引,想找出Code唯一的这些成员,那么这里就需要重新定义一个对Code比较的类,或者再扩展成泛型类,但是这样非常繁琐。
  3. 博客鹤冲天的改进办法(以下均转自这个博客)

    • 首先,创建一个通用比较的类,实现IEqualityComparer<T>接口:
    1. public class CommonEqualityComparer<T, V> : IEqualityComparer<T>
    2. {
    3. private Func<T, V> keySelector;
    4. public CommonEqualityComparer(Func<T, V> keySelector)
    5. {
    6. this.keySelector = keySelector;
    7. }
    8. public bool Equals(T x, T y)
    9. {
    10. return EqualityComparer<V>.Default.Equals(keySelector(x), keySelector(y));
    11. }
    12. public int GetHashCode(T obj)
    13. {
    14. return EqualityComparer<V>.Default.GetHashCode(keySelector(obj));
    15. }
    16. }
    • 借助上面这个类,Distinct扩展方法就可以这样写:
    1. public static class DistinctExtensions
    2. {
    3. public static IEnumerable<T> Distinct<T, V>(this IEnumerable<T> source, Func<T, V> keySelector)
    4. {
    5. return source.Distinct(new CommonEqualityComparer<T, V>(keySelector));
    6. }
    7. }
    • 下面的使用就很简单了:
    1. Product[] products =
    2. {
    3. new Product { Name = "Smallapple", Code = 9 },
    4. new Product { Name = "orange", Code = 4 },
    5. new Product { Name = "Bigapple", Code = 9 },
    6. new Product { Name = "lemon", Code = 12 }
    7. };
    8. var p1 = products.Distinct(p => p.Code);
    9. foreach (Product pro in p1)
    10. Console.WriteLine(pro.Name + "," + pro.Code);
    11. var p2 = products.Distinct(p => p.Name);
    12. foreach (Product pro in p2)
    13. Console.WriteLine(pro.Name + "," + pro.Code);
    • 可以看到,加上Linq表达式,可以方便的对自定义类的任意字段进行Distinct的处理。

C# Distinct方法的使用笔记的更多相关文章

  1. 重写类的Equals以及重写Linq下的Distinct方法

    当自定义一个类的时候,如果需要用到对比的功能,可以自己重写Equals方法,最整洁的方法是重写GetHashCode()方法. 但是,这个方法只适用于对象自身的对比(如if(a==b))以及字典下的C ...

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

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

  3. Linq的Distinct方法的扩展

    原文地址:如何很好的使用Linq的Distinct方法 Person1: Id=1, Name="Test1" Person2: Id=1, Name="Test1&qu ...

  4. 【C#】详解使用Enumerable.Distinct方法去重

    Enumerable.Distinct 方法 是常用的LINQ扩展方法,属于System.Linq的Enumerable方法,可用于去除数组.集合中的重复元素,还可以自定义去重的规则. 有两个重载方法 ...

  5. 如何使用Linq或EF来对数据去重——Distinct方法详解

    刚开始接触LINQ时使用distinct去重时和大家一样遇到了一些麻烦,很感谢 http://www.cnblogs.com/A_ming/archive/2013/05/24/3097062.htm ...

  6. 扩展Linq的Distinct方法动态根据条件进行筛选

    声明为了方便自己查看所以引用 原文地址:http://www.cnblogs.com/A_ming/archive/2013/05/24/3097062.html Person1: Id=1, Nam ...

  7. Linq Enumerable.Distinct方法去重

    Enumerable.Distinct 方法 是常用的LINQ扩展方法,属于System.Linq的Enumerable方法,可用于去除数组.集合中的重复元素,还可以自定义去重的规则. 有两个重载方法 ...

  8. 【转载】C#中通过Distinct方法对List集合进行去重

    在C#的List集合对象中,可以使用Distinct方法来对List集合元素进行去重,如果list集合内部元素为值类型,则Distinct方法根据值类型是否相等来判断去重,如果List集合内部元素为引 ...

  9. DISTINCT 方法用于返回唯一不同的值 。

    DISTINCT 方法用于返回唯一不同的值 . 例如: $Model->distinct(true)->field('name')->select(); 生成的SQL语句是: SEL ...

随机推荐

  1. [iOS基础控件 - 6.11.3] 私人通讯录Demo 控制器的数据传递、存储

    A.需求 1.搭建一个"私人通讯录"Demo 2.模拟登陆界面 账号 密码 记住密码开关 自动登陆开关 登陆按钮 3.退出注销 4.增删改查 5.恢复数据(取消修改)   这个代码 ...

  2. POJ 3673 Cow Multiplication (水题)

    题意:给你两个数,求所有的数位的积的和. 析:太水了,没的说,可以先输入边算,也可以最后再算,一样.. 代码如下: #include <cstdio> #include <strin ...

  3. hdu 4786 Fibonacci Tree (2013ACMICPC 成都站 F)

    http://acm.hdu.edu.cn/showproblem.php?pid=4786 Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) ...

  4. sqlserver表分区与调优与行列转换

    转自: http://www.cnblogs.com/knowledgesea/p/3696912.html http://www.open-open.com/lib/view/open1418462 ...

  5. 8天玩转并行开发——第一天 Parallel的使用

    转自:http://www.cnblogs.com/huangxincheng/archive/2012/04/02/2429543.html 随着多核时代的到来,并行开发越来越展示出它的强大威力,像 ...

  6. android ipc通信机制之之三,进程通讯方式。

    IPC通讯方式的优缺点: IPC通讯方式的对比 名称 优点 缺点 适用场景 Bundle 简单易用 只能传输Bundle支持的数据类型 四大组件的进程通信 文件共享 简单易用 不适合高并发场景,并无法 ...

  7. Minesweeper PC/UVa IDs: 110102/10189, Popularity: A,Success rate: high Level: 1

    #include<cstdio> #include<iostream> #include<string> #include<algorithm> #in ...

  8. PostgreSQL的initdb 源代码分析之五

    接前面,继续分析: putenv("TZ=GMT") 设置了时区信息. find_other_exec(argv[0], "postgres", PG_BACK ...

  9. cdoj 1255 斓少摘苹果 贪心

    斓少摘苹果 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1255 Descr ...

  10. Hdu 5568 sequence2 高精度 dp

    sequence2 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=556 ...