Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find elements 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 simplest way using linq to select values that exist in both collections?

i.e. a collection containing 2 and 4.

like image 858
maxp Avatar asked Jul 07 '11 10:07

maxp


1 Answers

int[] result = foo.Intersect(bar).ToArray();
like image 59
Nagg Avatar answered Oct 23 '22 06:10

Nagg