Intersect can be used to find matches between two collections, like so:
// 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
}
However what I'd like to achieve is the opposite, I'd like to list items from one collection that are missing from the other:
// Assign two arrays.
int[] array1 = { 1, 2, 3 };
int[] array2 = { 2, 3, 4 };
// Call "NonIntersect" 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
}
As stated, if you want to get 4 as the result, you can do like this:
var nonintersect = array2.Except(array1);
If you want the real non-intersection (also both 1 and 4), then this should do the trick:
var nonintersect = array1.Except(array2).Union( array2.Except(array1));
This will not be the most performant solution, but for small lists it should work just fine.