http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 3859|回复: 3

[C#语言基础] List之Union(),Intersect(),Except() 亦可以说是数学中的并集,交集,差集

[复制链接]
发表于 2014-12-31 11:03:50 | 显示全部楼层 |阅读模式
Union()
这个方法将会Union(并集)两个序列(集合)连接成一个新列表(集合)

方法定义是:
[C#] 纯文本查看 复制代码
public static IEnumerable<TSource> Union<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)

public static IEnumerable<TSource> Union<TSource>(this IEnumerable<TSource> first,IEnumerable<TSource> second, IEqualityComparer<TSource> comparer) 



Intersect ()

它将产生两个序列的交集.
方法定义是:
[C#] 纯文本查看 复制代码
public static IEnumerable<TSource> Intersect<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second) public static IEnumerable<TSource> Intersect<TSource>(this IEnumerable<TSource> first, Enumerable<TSource> second,IEqualityComparer<TSource> comparer) 

Except ()

它是从一个集合中删除存在另一个集合中的项.两个序列产生的集合差. 英文意思是:除此之外
方法定义是:
[C#] 纯文本查看 复制代码
public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second) 
public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)
实例代码分别如下:

[C#] 纯文本查看 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data; 


namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            IList<Student> oneStudents = new List<Student>();
            oneStudents.Add(new Student(1,false,"小新1","徐汇"));
            oneStudents.Add(new Student(2,false,"小新2","闵行"));
            oneStudents.Add(new Student(3, false, "小新3", "嘉定"));
            oneStudents.Add(new Student(4, false, "小新4", "闸北"));

            IList<Student> twoStudents = new List<Student>();
            twoStudents.Add(new Student(5, false, "小新5", "贵州"));
            twoStudents.Add(new Student(6, false, "小新6", "湖北"));
            twoStudents.Add(new Student(7, false, "小新7", "山东"));
            twoStudents.Add(new Student(8, false, "小新8", "西藏"));

            IList<Student> threeStudents = new List<Student>();
            threeStudents.Add(new Student(1, false, "小新1", "徐汇"));
            threeStudents.Add(new Student(2, false, "小新2", "闵行"));
            var bingji = oneStudents.Union(twoStudents, new StudentListEquality()).ToList();//并(全)集 
              var jiaoji = oneStudents.Intersect(threeStudents, new StudentListEquality()).ToList();//交集 
              var chaji = oneStudents.Except(threeStudents, new StudentListEquality()).ToList();//差集

              Console.WriteLine();
            Console.WriteLine("以下是并集的结果");            
            bingji.ForEach(x =>
            {
                Console.WriteLine(x.StudentId.ToString() + "    " + x.Sex.ToString() + "   " + x.Name.ToString()+" "+x.Address.ToString());
     
            });
            Console.WriteLine();
            Console.WriteLine("以下是交集的结果");           
            jiaoji.ForEach(x =>
            {
                Console.WriteLine(x.StudentId.ToString() + "    " + x.Sex.ToString() + "   " + x.Name.ToString() + " " + x.Address.ToString());

            });

            Console.WriteLine();
            Console.WriteLine("以下是差集的结果");            
            chaji.ForEach(x =>
            {
                Console.WriteLine(x.StudentId.ToString() + "    " + x.Sex.ToString() + "   " + x.Name.ToString() + " " + x.Address.ToString());

            });
        }

    }





    public class Student
    {
        public Student(int studentId, bool sex, String name, String address)
        {
            this.StudentId = studentId;
            this.Sex = sex;
            this.Name = name;
            this.Address = address;
        }
        public int StudentId { 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.StudentId == y.StudentId;
        }

        public int GetHashCode(Student obj)
        {
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return obj.ToString().GetHashCode();
            }
        }
    }



}
以上运行的结果是:
2012120319202813.png
以上的结果是重载了含有参数的IEqualityComparer<TSource> 方法,实现IEqualityComparer接口  对数据进行了重复过滤,如果不实现这个方法结果是
2012120319202813.png
[C#] 纯文本查看 复制代码
var bingji = oneStudents.Union(twoStudents).ToList();//并(全)集 
var jiaoji = oneStudents.Intersect(threeStudents).ToList();//交集 
var chaji = oneStudents.Except(threeStudents).ToList();//差集
但是对于List<T>的T是简单类型,如int  string  long 。。。。。是怎么样的呢?代码如下所示
[C#] 纯文本查看 复制代码
IList<int> firstNumbers = new List<int>()
             {
                 1,2,3,4,5,6,7

             };

            IList<int> secondNumbers = new List<int>()
             {

                 8,9,10

             };

            IList<int> thressNumbers = new List<int>()
             {

                 1,2,3

             };


            var result1 = firstNumbers.Union(secondNumbers).ToList();
            var result2 = firstNumbers.Intersect(thressNumbers).ToList();
            var result3 = firstNumbers.Except(thressNumbers).ToList();
            Console.WriteLine("以下是并集的结果");
            result1.ForEach(x => Console.WriteLine(x));

            Console.WriteLine();
            Console.WriteLine("以下是交集的结果");
            result2.ForEach(x => Console.WriteLine(x));

            Console.WriteLine();
            Console.WriteLine("以下是差集的结果");
            result3.ForEach(x => Console.WriteLine(x));

            Console.WriteLine("以上是简单类型如:int string long。。。。。没有实现IEqualityComparer<T>接口");
2012120319361045.png





1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2014-12-31 14:07:28 | 显示全部楼层
膜拜中....!
回复

使用道具 举报

发表于 2014-12-31 14:44:50 | 显示全部楼层
膜拜中....!
回复

使用道具 举报

发表于 2014-12-31 14:45:03 | 显示全部楼层
膜拜中....!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-20 08:11

© 2014-2021

快速回复 返回顶部 返回列表