两个列表的互补? [英] Complement of Two Lists?

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

问题描述

让我说我有一个字符串列表:

lets say I have a list of strings:

A,B,C,D

然后是另一个字符串列表

Then another list of strings

B,C,D

我想知道第一个列表中的哪些元素不在第二个列表中,所以结果将为A

I want to know what elements are in the first list that aren't in the second list, so the result would be A

我不知道执行此操作的扩展方法的名称.我知道我可以使用concat,union,intersect进行类似的列表比较,但是只是不知道要完成此特定任务的名称.

I don't know the name of the extension method to do this is. I know I can use concat, union, intersect for similar list comparisons, but just don't know the name to accomplish this particular task.

附录,我对重复项感兴趣,所以如果第一个列表是:

Addendum, I am interested in duplicates, so if the first list is:

A,A,A,B,C,D

A,A,A,B,C,D

第二个列表是

B,C,D

我想得到

A,A,A

谢谢!

推荐答案

您可以使用

You can use the Except Extension Method to get all elements in a list that are not in a second list:

var result = list1.Except(list2);

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

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