(see my code snippet below). I want to find all coll1 elements that match coll2 elements (number of coll2 elements <= number of coll1 elements) and put the query result in coll3. How to achieve this with linq expression and lambda? Of course, I can just copy coll2 to coll3 :-), but that is not my goal. I want to know how to use linq and lambda to replace such a conditional logical construct. Thank you in advance.
var coll1 = new List<int>() { 1, 2, 3, 4, 5 }; var coll2 = new List<int>() { 2, 4 }; var coll3 = new List<int>(); foreach ( var selected in coll2 ) { foreach ( var item in coll1 ) { if ( selected == item ) { coll3.Add(item); } } }
source share