c# - Intersecting any number of Lists -


say have number of lists

list<int> list1 = new list<int>(); list<int> list2 = new list<int>(); list<int> listn = new list<int>(); 

how can final list contains elements present in every list.

i did many loops. not satisfied. limited knowledge of linq think should easy don't know start.

is there other way this, preferably linq.

pretty simple:

var intersection = list1.intersect(list2).intersect(list3); 

or if want support n number of lists:

var lists = new list<list<string>>();  lists.add(new list<string>(new string[] { "test1", "test2", "test3" })); lists.add(new list<string>(new string[] { "test1", "test2", "test4" })); lists.add(new list<string>(new string[] { "test1", "test2", "test5" })); lists.add(new list<string>(new string[] { "test1", "test2", "test6" }));  var aggregate = lists.aggregate((x, y) => x.intersect(y).tolist()); 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -