获取" DIFF"在C#两个阵列之间? [英] Getting the "diff" between two arrays in C#?
问题描述
让我们说我有这两个数组:
Let's say I have these two arrays:
var array1 = new[] {"A", "B", "C"};
var array2 = new[] {"A", "C", "D"};
我想获得两者之间的差异。我知道我可以在code的短短几行写这篇文章,但我想确保我不会错过一个内置的语言特性或LINQ扩展方法。
I would like to get the differences between the two. I know I could write this in just a few lines of code, but I want to make sure I'm not missing a built in language feature or a LINQ extension method.
在理想情况下,我将结束与以下三个结果:
Ideally, I would end up with the following three results:
- 产品不是在数组1,但在数组2(D)
- 产品不是在数组2,但在ARRAY1(B)
- 产品是在两个
在此先感谢!
推荐答案
如果你有LINQ提供给你,你可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.linq.enumerable.except.aspx\"><$c$c>Except$c$c>和<一个href=\"http://msdn.microsoft.com/en-us/library/system.linq.enumerable.distinct.aspx\"><$c$c>Distinct$c$c>.你这个问题问的套分别是:
If you've got LINQ available to you, you can use Except
and Distinct
. The sets you asked for in the question are respectively:
- array2.Except(array1)
- array1.Except(array2)
- array1.Intersect(array2)
这篇关于获取&QUOT; DIFF&QUOT;在C#两个阵列之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!