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} \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; } }
分类:
C#.Net & MVC
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架