2

Say I've got an

Array1 [1,2,3]

and a List of arrays Array2 [3,2,4] Array3 [2,16,5] I need to return only those elements of the List which contain exactly two ints from Array1. In this case, Array2 since integers 2 and 3 intersect; Thanks

2 Answers 2

1

Try to combine Where() and Count():

var matches = new int[] { 1, 2, 3 };
var data = new List<int[]>
{
     new int[] { 3, 2, 4 },
     new int[] { 2, 16, 5 }
};

var result = data.Where(x => x.Count(matches.Contains) == 2);
Sign up to request clarification or add additional context in comments.

Comments

1

since it's int[] you can use the .Intersect() directly. For example

from a in arrays where a.Intersect(Array1).Count() == 2 select a
//arrays contains Array2 and Array3

2 Comments

What if that List in not a list of arrays but a list of objects each with an array as a field. Sorry if this is dumb but...
from a in objectsWithArray where a.ArrayProperty.Intesect(...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.