Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get array overlaps using LINQ?

Tags:

arrays

c#

linq

Suppose I have the following arrays:

List<int[]> numbers = new List<int[]>();
numbers.Add(new int[] { 1, 2, 3 });
numbers.Add(new int[] { 3, 4, 5 });
numbers.Add(new int[] { 5, 6, 7 });

int[] numbersToFind = new int[] { 4, 5, 6 };

I want to find which of the numbers elements contains one/more of the values in numbersToFind, is there an easy way of doing this with LINQ? That is, some code that would return a IEnumerable<int[]> containing an int[]{3,4,5} and int[]{5,6,7} in the above example.

How can this be done?

like image 561
Michael Low Avatar asked Nov 29 '22 05:11

Michael Low


1 Answers

numbers.Where(array => array.Any(value => numbersToFind.Contains(value)));
like image 186
cdhowie Avatar answered Dec 18 '22 10:12

cdhowie