获取的两个列表差异 [英] Get differences between two list

查看:115
本文介绍了获取的两个列表差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象类型的2列表:

I have a 2 lists of an object type:


List<MyClass> list1;
List<MyClass> list2;


什么是最好的方式(性能和洁净code)来提取这两个表之间的差异数据?
我的意思是获取被添加,删除或更改对象(的变化)?


What is the best way (performance and clean code) to extract differences in data between these two List?
I mean get objects that is added, deleted, or changed (and the change)?

推荐答案

尝试除了联盟,但你会需要为了找到这两个差异做两个。

Try Except with Union, but you'll need to do it for both in order to find differences in both.

var exceptions = list1.Except(list2).Union(list2.Except(list1)).ToList();

或作为LINQ的替代方案,有可能是一个更快的方法:HashSet.SymmetricExceptWith():

var exceptions = new HashSet(list1);

exceptions.SymmetricExceptWith(list2);

这篇关于获取的两个列表差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆