C#中的Linq Intersect与Except方法使用实例
作者:junjie 时间:2021-11-30 01:25:52
实例描述
现有某班学生的两份成绩,两份成绩中存在一些不一致的记录。需借助于编程方法找出这些不一致的记录。
实例代码
using System;
using System.Collections.Generic;
using System.Linq;
namespace IntersectAndExceptExp
{
class Program
{
static void Main(string[] args)
{
List<Student> studentList1 = newList<Student>() {
new Student(){StudentId=1,Score=64},
new Student(){StudentId=2,Score=85},
new Student(){StudentId=3,Score=78},
new Student(){StudentId=4,Score=94},
new Student(){StudentId=5,Score=90}
};
List<Student> studentList2 = newList<Student>() {
new Student(){StudentId=1,Score=64},
new Student(){StudentId=2,Score=80},
new Student(){StudentId=3,Score=78},
new Student(){StudentId=4,Score=94},
new Student(){StudentId=5,Score=95}
};
var both = studentList1.Intersect(studentList2,new StudentComparer());
var diff1 =studentList1.Except(both, new StudentComparer());
var diff2 =studentList2.Except(both, new StudentComparer());
Console.WriteLine("-------------下面是两份成绩中不同的记录--------------");
Console.WriteLine("-------------第一份学生成绩--------------");
foreach (var s in diff1)
{
Console.WriteLine("StudentId:"+s.StudentId+";Score:"+s.Score);
}
Console.WriteLine("-------------第一份学生成绩--------------");
foreach (var s in diff2)
{
Console.WriteLine("StudentId:"+ s.StudentId + ";Score:" + s.Score);
}
}
}
public class Student
{
public int StudentId { get; set; }
public int Score { get; set; }
}
public class StudentComparer : IEqualityComparer<Student>
{
public bool Equals(Student x, Studenty)
{
if (Object.ReferenceEquals(x, y)) returntrue;
return x != null && y != null&& x.StudentId == y.StudentId && x.Score == y.Score;
}
public int GetHashCode(Student obj)
{
int hashStudentId =obj.StudentId.GetHashCode();
int hashScore =obj.Score.GetHashCode();
return hashStudentId ^ hashScore;
}
}
}
代码说明
先使用Intersect方法生成两份记录的交集,该方法会使用传入的比较器对值进行比较决定记录是否相同。基于前步生成的交集,再使用Except方法找出两份记录中不一致的记录,该方法同样使用传入的比较器对值进行比较决定记录是否相同。
执行结果
标签:C#,Linq,Intersect,Except,方法
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java如何导出insert语句并生成sql脚本
2022-05-18 10:33:54
Java类之间的关系图_动力节点Java学院整理
2022-07-31 23:03:46
![](https://img.aspxhome.com/file/2023/9/68569_0s.jpg)
SpringBoot2零基础到精通之映射与常用注解请求处理
2022-06-11 15:41:51
![](https://img.aspxhome.com/file/2023/1/62141_0s.jpg)
Java xml出现错误 javax.xml.transform.TransformerException: java.lang.NullPointerException
2023-11-08 14:48:13
通过实例解析Spring argNames属性
2023-09-14 10:43:13
![](https://img.aspxhome.com/file/2023/0/58540_0s.jpg)
利用ssh实现服务器文件上传下载
2021-10-16 09:28:47
JAVA Integer类常用方法解析
2021-09-01 06:51:08
java 使用异常的好处总结
2023-11-29 13:35:49
MyBatis一对多嵌套查询的完整实例
2023-07-12 02:49:56
Java面向对象基础知识之委托和lambda
2022-07-28 16:51:11
Java @Deprecated注解的作用及传递性
2023-08-11 12:55:05
![](https://img.aspxhome.com/file/2023/7/58097_0s.png)
JAVA包装类及自动封包解包实例代码
2022-10-23 13:28:54
Java实现调用外部程序的示例代码
2023-11-10 06:43:12
![](https://img.aspxhome.com/file/2023/2/59212_0s.png)
教你用Java在个人电脑上实现微信扫码支付
2023-07-22 20:52:15
![](https://img.aspxhome.com/file/2023/2/57722_0s.png)
如何使用Java redis实现发送手机验证码功能
2023-11-26 17:25:00
Java内部类知识汇总
2023-08-18 14:06:54
![](https://img.aspxhome.com/file/2023/0/61000_0s.png)
Java实现三子棋小游戏
2022-09-12 01:27:20
Spring Boot深入排查 java.lang.ArrayStoreException异常
2023-07-11 16:31:27
关于java关键字this和super的区别和理解
2022-08-01 14:33:09
![](https://img.aspxhome.com/file/2023/2/63052_0s.png)
Springboot Vue可配置调度任务实现示例详解
2023-11-09 03:33:19