List Distinct

list集合的去重操作

代码

    class ListDistinctDemo
    {
        static void Main(string[] args)
        {
            List<Person> personList = new List<Person>(){
                new Person(3),//重复数据
                new Person(3),
                new Person(2),
                new Person(1)
            };

            //使用匿名方法
            List<Person> delegateList = personList.Distinct(new Compare<Person>(
                delegate(Person x, Person y)
                {
                    if (null == x || null == y) return false;
                    return x.ID == y.ID;
                })).ToList();

            delegateList.ForEach(s => Console.WriteLine(s.ID));

            //使用 Lambda 表达式
            List<Person> lambdaList = personList.Distinct(new Compare<Person>(
                (x, y) => (null != x && null != y) && (x.ID == y.ID))).ToList();

            lambdaList.ForEach(s => Console.WriteLine(s.ID));

            //排序
            personList.Sort((x, y) => x.ID.CompareTo(y.ID));
            personList.ForEach(s => Console.WriteLine(s.ID));

        }
    }
    public class Person
    {
        public int ID { get; set; }
        public string Name { get; set; }

        public Person(int id)
        {
            this.ID = id;
        }
    }

    public delegate bool EqualsComparer<T>(T x, T y);

    public class Compare<T> : IEqualityComparer<T>
    {
        private EqualsComparer<T> _equalsComparer;

        public Compare(EqualsComparer<T> equalsComparer)
        {
            this._equalsComparer = equalsComparer;
        }

        public bool Equals(T x, T y)
        {
            if (null != this._equalsComparer)
                return this._equalsComparer(x, y);
            else
                return false;
        }

        public int GetHashCode(T obj)
        {
            return obj.ToString().GetHashCode();
        }
    }
时间: 2024-10-29 19:09:37

List Distinct的相关文章

spark 教程三 spark Map filter flatMap union distinct intersection操作

RDD的创建 spark 所有的操作都围绕着弹性分布式数据集(RDD)进行,这是一个有容错机制的并可以被并行操作的元素集合,具有只读.分区.容错.高效.无需物化.可以缓存.RDD依赖等特征 RDD的创建基础RDD 1.并行集合(Parallelized Collections):接收一个已经存在的Scala集合,然后进行各种并行运算 var sc=new SparkContext(conf) var rdd=sc.parallelize(Array(2,4,9,3,5,7,8,1,6)); rd

mysql count distinct 统计结果去重

mysql的sql语句中,count这个关键词能统计表中的数量,如 有一个tableA表,表中数据如下: id name age 1 tony 18 2 jacky 19 3 jojo 18 SELECT COUNT(age) FROM tableA 以上这条语句能查出table表中有多少条数据.查询结果是3 而COUNT这个关键词与 DISTINCT一同使用时,可以将统计的数据中某字段不重复的数量. 如: SELECT COUNT(DISTINCT age) from tableA 以上语句的

SQL中distinct的用法

1.作用于单列 2.作用于多列 3.COUNT统计 4.distinct必须放在开头 5.其他 在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值.关键词 distinct用于返回唯一不同的值. 表A: 表B: 1.作用于单列 select distinct name from A 执行后结果如下: 2.作用于多列 示例2.1 select distinct name, id from A 执行后结果如下: 实际上是根据name和id两个字段来去重的

C# 中distinct的使用

假设我们有一个类:Product public class Product { public string Id { get; set; } public string Name { get; set; } } Main函数如下: static void Main() { List<Product> products = new List<Product>() { new Product(){ Id="1", Name="n1"}, new

PostgreSQL 的 distinct on 的理解

摘录自:http://www.cnblogs.com/gaojian/archive/2012/09/05/2671381.html 对于 select distinct on , 可以利用下面的例子来理解: create table a6(id integer, name varchar(10)); insert into a6 values(1, ' 001'); insert into a6 values(1, '002'); insert into a6 values(2, '003')

Distinct Subsequences

https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be non

Oracle的去重函数 distinct

原贴地址:http://www.cnblogs.com/rainman/archive/2013/05/03/3058451.html#m0 SQL中distinct的用法 1.作用于单列 2.作用于多列 3.COUNT统计 4.distinct必须放在开头 5.其他 在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值.关键词 distinct用于返回唯一不同的值. 表A: 表B: 1.作用于单列 select distinct name from

LeetCode115 Distinct Subsequences

Given a string S and a string T, count the number of distinct subsequences of T in S. (Hard) A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the re

[LeetCode] Distinct Subsequences 解题思路

Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative

SPOJ 题目694 Distinct Substrings(后缀数组,求不同的子串个数)

DISUBSTR - Distinct Substrings no tags Given a string, we need to find the total number of its distinct substrings. Input T- number of test cases. T<=20; Each test case consists of one string, whose length is <= 1000 Output For each test case output