C#高级:利用LINQ进行实体列表的集合运算

问题引入:

bash 复制代码
Teacher实体的唯一标识符是Name和Classes字段(或者说这两个字段唯一确定一条数据),如何对两个实体列表做交集、差集运算呢?(并集直接调用AddRange方法即可)

一、重写方法实现

1.原理

重写 Equals() 和 GetHashCode() 方法,使用Intersect() 和 Except() 方法在比较时就会基于这些字段来判断对象是否相等

2.快速生成代码

3.代码

cs 复制代码
public class Teacher
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Classes { get; set; }

    public override bool Equals(object obj)
    {
        return obj is Teacher teacher &&
               Name == teacher.Name &&
               Classes == teacher.Classes;
    }

    public override int GetHashCode()
    {
        return HashCode.Combine(Name, Classes);
    }
}

class Program
{
    static void Main()
    {
        // 创建两个示例列表
        List<Teacher> listA = new List<Teacher>
        {
            new Teacher { Id = 1, Name = "Alice", Classes = "Math" },
            new Teacher { Id = 2, Name = "Bob", Classes = "Science" },
            new Teacher { Id = 3, Name = "Charlie", Classes = "English" }
        };

        List<Teacher> listB = new List<Teacher>
        {
            new Teacher { Id = 2, Name = "Bob", Classes = "Science" },
            new Teacher { Id = 3, Name = "Charlie", Classes = "English" },
            new Teacher { Id = 4, Name = "David", Classes = "History" }
        };

        // 求交集
        var intersection = listA.Intersect(listB).ToList();

        // 求差集 A - B
        var difference = listA.Except(listB).ToList();


        // 输出结果
        Console.WriteLine("交集:");
        foreach (var teacher in intersection)
        {
            Console.WriteLine($"Id: {teacher.Id}, Name: {teacher.Name}, Classes: {teacher.Classes}");
        }

        Console.WriteLine("\n差集 A - B:");
        foreach (var teacher in difference)
        {
            Console.WriteLine($"Id: {teacher.Id}, Name: {teacher.Name}, Classes: {teacher.Classes}");
        }
    }
}

二、嵌套查询实现

1.原理

cs 复制代码
var intersection = listA.Where(a => listB.Any(b => b.Name == a.Name && b.Classes == a.Classes)).ToList();
var difference = listA.Where(a => !listB.Any(b => b.Name == a.Name && b.Classes == a.Classes)).ToList();

where-Any相当于A表内连接B表( A ∩ B );

where-!Any相当于A表左外连接B表并剔除匹配到的数据(A - B)。

2.代码

cs 复制代码
public class Teacher
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Classes { get; set; }
}

class Program
{
    static void Main()
    {
        // 创建两个示例列表
        List<Teacher> listA = new List<Teacher>
        {
            new Teacher { Id = 1, Name = "Alice", Classes = "Math" },
            new Teacher { Id = 2, Name = "Bob", Classes = "Science" },
            new Teacher { Id = 3, Name = "Charlie", Classes = "English" }
        };

        List<Teacher> listB = new List<Teacher>
        {
            new Teacher { Id = 2, Name = "Bob", Classes = "Science" },
            new Teacher { Id = 3, Name = "Charlie", Classes = "English" },
            new Teacher { Id = 4, Name = "David", Classes = "History" }
        };

        // 自定义方法来比较 Name 和 Classes
        var intersection = listA.Where(a => listB.Any(b => b.Name == a.Name && b.Classes == a.Classes)).ToList();
        var difference = listA.Where(a => !listB.Any(b => b.Name == a.Name && b.Classes == a.Classes)).ToList();

        // 输出交集
        Console.WriteLine("交集:");
        foreach (var teacher in intersection)
        {
            Console.WriteLine($"Id: {teacher.Id}, Name: {teacher.Name}, Classes: {teacher.Classes}");
        }

        // 输出差集
        Console.WriteLine("\n差集 A - B:");
        foreach (var teacher in difference)
        {
            Console.WriteLine($"Id: {teacher.Id}, Name: {teacher.Name}, Classes: {teacher.Classes}");
        }
    }
}
相关推荐
勘察加熊人3 小时前
forms实现俄罗斯方块
c#
艾妮艾妮7 小时前
C语言常见3种排序
java·c语言·开发语言·c++·算法·c#·排序算法
小码编匠7 小时前
.NET 验证码生成神器基于 SkiaSharp 的高性能方案
后端·c#·.net
专注VB编程开发20年8 小时前
Aspose.words,Aspose.cells,vb.net,c#加载许可证,生成操作选择:嵌入的资源
c#·word·.net·vb.net
andy55208 小时前
.NET 使用 WMQ 连接Queue 发送 message 实例
xml·c#·wmq·c# 连接wmq·发送消息到wmq
破罐子不摔8 小时前
【C#使用S7.NET库读取和写入西门子PLC变量】
java·c#·.net
杰尼杰尼丶8 小时前
Winform MQTT客户端连接方式
c#·winform
weixin_307779139 小时前
C#实现HiveQL建表语句中特殊数据类型的包裹
开发语言·数据仓库·hive·c#
lixy5799 小时前
C# WPF 命令机制(关闭CanExecute自动触发,改手动)
c#·wpf
天地长久.9 小时前
C# N层架构和解耦
c#·解耦·多层架构