Search for the intersection of any number of lists
If you want to find the set of numbers that exist in all lists, then Enumerable.Intersect is a good way to do this. You donβt even need to hardcode the collection of lists; you can create it at runtime:
var lists = new[] { list1, list2, ..., listN };
Search for the union of intersections between the main list and each other
If you want to find a set that includes all the common numbers between list 1 and list 2, combine all the common numbers between list 1 and list N, and then change a few:
var common = Enumerable.Empty<int>(); foreach (var list in lists.Skip(1)) { common = common.Union(lists.First().Intersect(list)); }
source share