zoukankan      html  css  js  c++  java
  • C# List使用District去重复数据

    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();
            }
        }
  • 相关阅读:
    BootstrapTable表格数据左右移动功能遇到的问题(数据左右移动,列表拖拽排序,模糊查询列表数据定位)
    MVC校验
    线程
    验证码
    PublicLogic
    进程
    请求处理过程
    上传组件
    委托
    Global全局应用程序类
  • 原文地址:https://www.cnblogs.com/mschen/p/5126949.html
Copyright © 2011-2022 走看看