# Intersect（）的相反

``// Assign two arrays. int[] array1 = { 1, 2, 3 }; int[] array2 = { 2, 3, 4 }; // Call Intersect extension method. var intersect = array1.Intersect(array2); // Write intersection to screen. foreach (int value in intersect) { Console.WriteLine(value); // Output: 2, 3 }` `

` `// Assign two arrays. int[] array1 = { 1, 2, 3 }; int[] array2 = { 2, 3, 4 }; // Call Intersect extension method. var intersect = array1.NonIntersect(array2); // I've made up the NonIntersect method // Write intersection to screen. foreach (int value in intersect) { Console.WriteLine(value); // Output: 4 }` `

` `var nonintersect = array2.Except(array1);` `

` `var nonintersect = array1.Except(array2).Union( array2.Except(array1));` `

` `a.Except(b).Union(b.Except(a));` `

` `var difference = new HashSet(a); difference.SymmetricExceptWith(b);` `

` `public static IEnumerable<T> SymmetricExcept<T>(this IEnumerable<T> seq1, IEnumerable<T> seq2) { HashSet<T> hashSet = new HashSet<T>(seq1); hashSet.SymmetricExceptWith(seq2); return hashSet.Select(x => x); }` `

Except运算符产生两个序列之间的设定差异。 它只会返回第一个序列中没有出现在第二个序列中的元素。 您可以select提供您自己的相等比较function。

B \ A（B中所有不在A中出现的东西）？

` `/// <summary> /// Given two list, compare and extract differences /// http://stackoverflow.com/questions/5620266/the-opposite-of-intersect /// </summary> public class CompareList { /// <summary> /// Returns list of items that are in initial but not in final list. /// </summary> /// <param name="listA"></param> /// <param name="listB"></param> /// <returns></returns> public static IEnumerable<string> NonIntersect( List<string> initial, List<string> final) { //subtracts the content of initial from final //assumes that final.length < initial.length return initial.Except(final); } /// <summary> /// Returns the symmetric difference between the two list. /// http://en.wikipedia.org/wiki/Symmetric_difference /// </summary> /// <param name="initial"></param> /// <param name="final"></param> /// <returns></returns> public static IEnumerable<string> SymmetricDifference( List<string> initial, List<string> final) { IEnumerable<string> setA = NonIntersect(final, initial); IEnumerable<string> setB = NonIntersect(initial, final); // sum and return the two set. return setA.Concat(setB); } }` `

array1.NonIntersect（数组2）;

` `a.except(b).union(b.Except(a));` `
` `string left = "411329_SOFT_MAC_GREEN"; string right= "SOFT_MAC_GREEN"; string[] l = left.Split('_'); string[] r = right.Split('_'); string[] distinctLeft = l.Distinct().ToArray(); string[] distinctRight = r.Distinct().ToArray(); var commonWord = l.Except(r, StringComparer.OrdinalIgnoreCase) string result = String.Join("_",commonWord); result = "411329"` `