C#比较两个list集合,两集合同时存在或A集合存在B集合中无
来源:互联网 发布:淘宝双十一并发量峰值 编辑:程序博客网 时间:2024/05/18 13:23
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} \t {1} \t {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} \t {1} \t {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; } }
阅读全文
0 0
- C#比较两个list集合,两集合同时存在或A集合存在B集合中无
- C#比较两个list集合,两集合同时存在或A集合存在B集合中无
- 筛选list集合中是否存在要比较的对象
- 有3个集合, 从其中一个集合中删除同时存在于另外两个集合的元素
- Oracle数据库求存在集合a中但不存在集合b中的数据
- 怎么判断java中一个元素存在list集合中
- 如何判断一个字符串是否在list集合中存在!
- Java判定数组或集合中是否存在某元素
- jsp中使用<c:foreach>同时遍历两个list集合
- 集合A 两两一组 重新组合成集合B
- list集合两种遍历效能比较
- 比较两个list集合是否相等
- 对集合中元素,求是否存在两个元素之和大小为X的两种做法
- Java判断一个值,或者对象是否存在list集合中和去掉list集合中重复的元素
- 比较两个集合
- 两个集合如何比较
- 比较两个有序集合
- A、B两个整数集合的交集
- leetcode -- 12. Integer to Roman
- android Log日志输出
- elasticsearch学习总结(三) API的使用范例6
- Hbase原理、基本概念、基本架构
- mysql 多主多从
- C#比较两个list集合,两集合同时存在或A集合存在B集合中无
- asp net core 跨平台初体验
- 代码格式
- 阿里云首推音乐变速短视频SDK,上线抖音 freestyle只需1步
- iOS-UIWebview设置超时、加载失败、重新加载等问题解决
- 工作日记2017.07.25
- python3.* No module named ‘MySQLdb'
- 再三强调直流电机无刷系列的特点和优势
- 4个你未必知道的内存小知识