Find items existing in both collections using LINQ

Say I have two collections

int[] foo = { 1, 2, 3, 4 }; int[] bar = { 2, 4, 6, 8 }; 

What would be the easiest way to use linq to select values โ€‹โ€‹that exist in both collections?

i.e. collection containing 2 and 4.

+8
c # linq intersection
source share
1 answer
 int[] result = foo.Intersect(bar).ToArray(); 
+17
source share

All Articles