Linq在两个列表中找到差异 [英] Linq find differences in two lists

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

问题描述

我有两个这样的成员列表:

I have two list of members like this:

之前:彼得,肯,朱莉娅,汤姆

Before: Peter, Ken, Julia, Tom

之后:彼得,罗伯特,茱莉亚,汤姆

After: Peter, Robert, Julia, Tom

如您所见,肯不在,罗伯特在.

As you can see, Ken is is out and Robert is in.

我想要的是检测更改.我想要列出两个列表中已更改的内容. linq可以如何帮助我?

What I want is to detect the changes. I want a list of what has changed in both lists. How can linq help me?

推荐答案

您的问题尚未完全指定,但我假设您正在寻找差异作为集合(也就是说,排序无所谓).如果是这样,则需要两个集合的对称差异.您可以使用 Enumerable.Except 来实现:

Your question is not completely specified but I assume that you are looking for the differences as sets (that is, ordering does not matter). If so, you want the symmetric difference of the two sets. You can achieve this using Enumerable.Except:

before.Except(after).Union(after.Except(before));

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

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