maxp
maxp

Reputation: 25181

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.

Upvotes: 8

Views: 2444

Answers (1)

EgorBo
EgorBo

Reputation: 6152

int[] result = foo.Intersect(bar).ToArray();

Upvotes: 17

Related Questions