zoukankan      html  css  js  c++  java
  • C#比较两个list集合,两集合同时存在或A集合存在B集合中无

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace Sample3
    {
        class Program
        {
            static void Main(string[] args)
            {
                var student1 = new List<student>();
                student1.Add(new student() { name = "张三", subject = "英语", score = 89 });
                student1.Add(new student() { name = "李四", subject = "英语", score = 95 });
                student1.Add(new student() { name = "王五", subject = "英语", score = 69 });
                student1.Add(new student() { name = "李倩", subject = "英语", score = 99 });
    
                var student2 = new List<student>();
                student2.Add(new student() { name = "李四", subject = "英语", score = 95 });
                student2.Add(new student() { name = "王五", subject = "数学", score = 69 });
                student2.Add(new student() { name = "赵六", subject = "数学", score = 100 });
    
                //var exp1 = student1.Where(a => student2.Any(t => a.name.Contains(t.name))).ToList();
                //使用Exists同样可以实现 字面上应该更好理解,而且效率要高些
                var exp1 = student1.Where(a => student2.Exists(t => a.name.Contains(t.name))).ToList();
                Console.WriteLine("--查找student1和student2总同时存在的数据--");
                foreach (var item in exp1)
                {
                    Console.WriteLine("{0} 	 {1} 	 {2}", item.name, item.subject, item.score);
                }
    
                //var exp2 = student1.Where(a => student2.All(t => !a.name.Contains(t.name))).ToList();  
                //使用Exists同样可以实现 字面上应该更好理解,而且效率要高些
                var exp2 = student1.Where(a => !student2.Exists(t => a.name.Contains(t.name))).ToList();
                Console.WriteLine("--查找student1集合中存在,而student2不存在的数据--");
                foreach (var item in exp2)
                {
                    Console.WriteLine("{0} 	 {1} 	 {2}", item.name, item.subject, item.score);
                }
                Console.Read();
    
                /*
                    --查找student1和student2总同时存在的数据--
                    李四     英语    95
                    王五     英语    69
                    --查找student1集合中存在,而student2不存在的数据--
                    张三     英语    89
                    李倩     英语    99 
                 */
            }
        }
    
        public class student
        {
            /// <summary>  
            /// 姓名  
            /// </summary>  
            public string name;
            /// <summary>  
            /// 科目  
            /// </summary>  
            public string subject;
            /// <summary>  
            /// 分数  
            /// </summary>  
            public int score;
        }  
    }
  • 相关阅读:
    springcloud配置eureka后启动application失败原因Error starting Tomcat context. Exception: org.springframework.beans.factory.UnsatisfiedDependencyException. Message: Error creating bean with name 'trace
    Parameter index out of range (1 > number of parameters, which is 0
    mysql返回某月份的第一天
    mybatisplus的自定义sql使用方法及VO的使用
    mysql截取地址为城市名
    mybatisplus多表查询及自定义查询
    mybatisplus条件构造器wrapper
    HTTP 【值得你看个究竟】
    Kafka 【入门一篇文章就够了】
    notepad++ 删除末尾换行符
  • 原文地址:https://www.cnblogs.com/smartsmile/p/7675572.html
Copyright © 2011-2022 走看看