• List之Union(),Intersect(),Except() 即并集,交集,差集运算。


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Sample2
    {
        class Program
        {
            static void Main(string[] args)
            {
                //List之Union(),Intersect(),Except() 即并集,交集,差集运算
                IList<Student> Students1 = new List<Student>();
                Students1.Add(new Student(1, false, "张三", "深圳"));
                Students1.Add(new Student(2, false, "李四", "广州"));
                Students1.Add(new Student(3, false, "王五", "珠海"));
                Students1.Add(new Student(4, false, "赵六", "东莞"));
    
                IList<Student> Students2 = new List<Student>();
                Students2.Add(new Student(1, false, "张三", "深圳"));
                Students2.Add(new Student(5, false, "李七", "广州"));
                Students2.Add(new Student(6, false, "孙八", "深圳"));
                Students2.Add(new Student(7, true, "赵燕", "深圳"));
    
                //自定义比较规则
                var bingji = Students1.Union(Students2, new StudentListEquality()).ToList();//并集(AB集合所有项)
                var jiaoji = Students1.Intersect(Students2, new StudentListEquality()).ToList();//交集 (AB集合共同项)
                var chaji = Students1.Except(Students2, new StudentListEquality()).ToList();//差集(A集合有,B没有)
    
                Console.WriteLine("以下是并集的结果");
                bingji.ForEach(x =>
                {
                    Console.WriteLine(x.Id.ToString() + "    " + x.Sex.ToString() + "   " + x.Name.ToString() + " " + x.Address.ToString());
    
                });
    
                Console.WriteLine("以下是交集的结果");
                jiaoji.ForEach(x =>
                {
                    Console.WriteLine(x.Id.ToString() + "    " + x.Sex.ToString() + "   " + x.Name.ToString() + " " + x.Address.ToString());
    
                });
    
                Console.WriteLine("以下是差集的结果");
                chaji.ForEach(x =>
                {
                    Console.WriteLine(x.Id.ToString() + "    " + x.Sex.ToString() + "   " + x.Name.ToString() + " " + x.Address.ToString());
    
                });
                Console.ReadKey();
            }
        }
    
        public class Student
        {
            public Student(int id, bool sex, String name, String address)
            {
                this.Id = id;
                this.Sex = sex;
                this.Name = name;
                this.Address = address;
            }
            public int Id { get; set; }
            public bool Sex { get; set; }
            public String Name { get; set; }
            public String Address { get; set; }
    
        }
    
        public class StudentListEquality : IEqualityComparer<Student>
        {
            public bool Equals(Student x, Student y)
            {
                return x.Id == y.Id && x.Name == y.Name && x.Address == y.Address && x.Sex == y.Sex;
            }
            public int GetHashCode(Student obj)
            {
                return (obj == null) ? 0 : obj.ToString().GetHashCode();
            }
        }
    }


  • 相关阅读:
    [Form Builder]POST 与 commit_form 的区别
    [Form Builder]Form中的validate验证事件
    [Form Builder]Oracle Form系统变量中文版总结大全
    [Form Builder]NAME_IN()与COPY()
    [Form Builder]APP_ITEM_PROPERTY.SET_PROPERTY 用法
    解决MVC模式文件下载附件中文名称乱码
    [ASP.NET MVC]笔记(四) UnobtruSive AJAX和客户端验证
    log4net的使用
    Linq 实现sql中的not in和in条件查询
    [ASP.NET MVC]笔记(三) 成员资格、授权和安全性
  • 原文地址:https://www.cnblogs.com/smartsmile/p/6234042.html
Copyright © 2020-2023  润新知