我有两个系列,比较它们并希望得到无与伦比的项目并放入一个新的系列.我不想使用传统方法使用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
Run Code Online (Sandbox Code Playgroud)
编辑:抱歉,我忘记提及:collection2是collection1的子集,因此collection2中的所有元素必须存在于collection1中.
像这样的东西?
int[] collection3 = collection1.Except(collection2).ToArray();
Run Code Online (Sandbox Code Playgroud)