使用LINQ查找两个集合中存在的元素

max*_*axp 8 .net c# linq intersection

说我有两个收藏

int[] foo = { 1, 2, 3, 4 };
int[] bar = { 2, 4, 6, 8 };
Run Code Online (Sandbox Code Playgroud)

使用linq选择两个集合中存在的值的最简单方法是什么?

即包含2和4的集合.

Nag*_*agg 17

int[] result = foo.Intersect(bar).ToArray();
Run Code Online (Sandbox Code Playgroud)