【发布时间】:2013-07-23 15:36:32
【问题描述】:
我有两个集合,比较它们并希望得到不匹配的项目并放入一个新集合中。我不想使用使用 2 foreach 循环的传统方法。如何使用 linq 和 lambda 表达式来实现它? 例如
int[] collection1 = new int[] { 1, 2, 3, 4, 5 };
int[] collection2 = new int[] { 2, 3 };
// Goal: Using Linq and Lambda Expression: the new collection3 should contain 1, 4, 5
已编辑:抱歉我忘了提:collection2 是 collection1 的子集,因此 collection2 中的所有元素都必须存在于 collection1 中。
【问题讨论】:
-
请澄清:如果集合是
{1,2}和{2,3},结果应该是什么? -
嗨,Henrik,谢谢。我已经编辑了。