Search code examples
c#linqlambda

how to search a List<list<int>> with another List<int> in c#


I have the following lists of list.

List<List<int>> paths = new List<List<int>>();
paths.Add(new List<int>() { 0,1 });
paths.Add(new List<int>() { 0, 2 });
paths.Add(new List<int>() { 0, 4 });
paths.Add(new List<int>() { 1, 2 });
paths.Add(new List<int>() { 0, 3 });

and I have another List of int like this

List<int> ends = new List<int>(){3,4};

Now I need to filter records where any number in the inner list in paths contains any numbers from the ends list, which for this example would be the following records.

{0,3}
{0,4} 

I tried something like this

paths.Where(x => x.Where(y => ends.Contains(y))).ToList();

Solution

  •  paths.Where(x => x.Any(z => ends.Contains(z)))