使用LINQ、Lambda 表达式 、委托快速比较两个集合,找出需要新增、修改、删除的对象

快速对比两个list数据集合

此文引用csdn :https://blog.csdn.net/Zhu_daye/article/details/104798410

小批量、快速对比两个list数据集合
using System.Linq.Expressions;

Main();
void Main()
{
    // 对比源集合
    var source = GenerateStudent(1, 10000, 1000);
    // 目标集合
    var target = GenerateStudent(5000, 10000, 1000);
    //Expression<>
    // 唯一标识比较
    Func<Student, Student, bool> keyCompartor = (s, t) => s.Id == t.Id;
    // 实体相等比较
    Func<Student, Student, bool> entityCompartor = (s, t) => s.Id == t.Id && s.Name.Equals(t.Name) && s.Age == t.Age;

    // 新增前准备
    Func<Student, Student> insertAction = (s) =>
    {
        return new Student
        {
            Id = s.Id,
            Name = s.Name,
            Age = s.Age,
            Operation = "Insert"
        };
    };

    // 更新前准备
    Func<Student, Student, Student> updateAction = (s, t) =>
    {
        t.Name = s.Name;
        t.Age = s.Age;
        t.Operation = "Update";

        return t;
    };

    // 删除前准备
    Func<Student, Student> deleteAction = (t) =>
    {
        t.Operation = "Delete";
        return t;
    };

    // 去掉相等对象
    RemoveDuplicate(source, target, entityCompartor, (s1, s2) => s1.Id == s2.Id, keyCompartor);

    // 需要新增的集合
    var insertingStudents = GetInsertingEntities(source, target, keyCompartor, insertAction);
    // 需要更新的集合
    var updatingStudents = GetUpdatingEntities(source, target, keyCompartor, entityCompartor, updateAction);
    // 需要删除的集合
    var deletingStudents = GetDeletingEntities(source, target, keyCompartor, deleteAction);

    // 后续业务
    // InsertStudents(insertingStudents);
    // UpdateStudents(updatingStudents);
    // DeleteStudents(deletingStudents);

    Console.WriteLine("");
    // 集合去重
    void RemoveDuplicate<S, T>(List<S> source, List<T> target, Func<S, T, bool> entityCompartor,
       Func<S, S, bool> sourceKeyCompartor, Func<S, T, bool> keyComportor)
    {
        var sameEntities = source.Where(s => target.Exists(t => entityCompartor(s, t))).ToList();
        source.RemoveAll(s => sameEntities.Exists(s2 => sourceKeyCompartor(s, s2)));
        target.RemoveAll(t => sameEntities.Exists(s => keyComportor(s, t)));
    }

    // 获取需要新增的对象集合
    List<T> GetInsertingEntities<S, T>(List<S> source, List<T> target, Func<S, T, bool> keyComportor,
       Func<S, T> insertAction)
    {
        var result = new List<T>();
        foreach (var s in source)
        {
            var t = target.FirstOrDefault(x => keyComportor(s, x));
            if (t == null)
            {
                // 目标集合中不存在,则新增
                result.Add(insertAction(s));
            }
        }

        return result;
    }

    // 获取需要更新的对象集合
    List<T> GetUpdatingEntities<S, T>(List<S> source, List<T> target, Func<S, T, bool> keyComportor,
       Func<S, T, bool> entityCompartor, Func<S, T, T> updateAction)
    {
        var result = new List<T>();
        foreach (var s in source)
        {
            var t = target.FirstOrDefault(x => keyComportor(s, x));
            if (t != null && !entityCompartor(s, t))
            {
                // 目标集合中存在,但是次要属性不相等,则更新
                result.Add(updateAction(s, t));
            }
        }

        return result;
    }

    // 获取需要删除的对象集合
    List<T> GetDeletingEntities<S, T>(List<S> source, List<T> target,
       Func<S, T, bool> keyComportor, Func<T, T> deleteAction)
    {
        var result = new List<T>();
        foreach (var t in target)
        {
            var s = source.FirstOrDefault(x => keyComportor(x, t));
            if (s == null)
            {
                // 源集合中存在,目标集合中需要删除
                result.Add(deleteAction(t));
            }
        }

        return result;
    }

    // 随机生成测试集合
    List<Student> GenerateStudent(int minId, int maxId, int maxNumber)
    {
        var r = new Random();
        var students = new List<Student>();
        for (int i = 0; i < maxNumber; i++)
        {
            students.Add(new Student
            {
                Id = r.Next(minId, maxId),
                Name = $"name: {r.Next(1, 10)}",
                Age = r.Next(6, 10)
            });
        }

        return students.GroupBy(s => s.Id).Select(s => s.First()).ToList();
    }
}
public class Student
{
    public int Id { get; set; }

    public string Name { get; set; }

    public int Age { get; set; }

    public string Operation { get; set; }
}

小数量数据 推荐使用linq 以下方法

点击查看代码
List<SRModel> SRlist= db.SRModels.ToList();
 List<SRUserInfoDto> Userlist = DataProcessing();
 //对比两个集合中的差异并出来。两种方法取。
var srGorup = Userlist.Where(x => !SRlist.Any(a => x.XiaoShouDaiBiaoBianMa == a.XiaoShouDaiBiaoBianMa)).ToList();  
 var intolist = Userlist.Where(a => !SRlist.Select(b => b.XiaoShouDaiBiaoBianMa).Contains(a.XiaoShouDaiBiaoBianMa)).ToList

IEqualityComparer 接口实现数据不同对比

可以参考 :https://www.cnblogs.com/zhaoshujie/p/14724710.html

posted @ 2024-01-21 20:13  Bo-Hong  阅读(78)  评论(0编辑  收藏  举报