LINQ query to find if items in a list are contained in another list

For those who came after reading heading of the question :

bool doesL1ContainsL2 = l1.Intersect(l2).Count() == l2.Count;

L1 and L2 are both List<T>

A simple explanation is : If resulting Intersection of two iterables has the same length as that of the smaller list (L2 here) ,then all the elements must be there in bigger list (L1 here)


For those who read the whole question

var list3 =  test2.Where(x => !test1.Any(y => x.Contains(y)));

var output = emails.Where(e => domains.All(d => !e.EndsWith(d)));

Or if you prefer:

var output = emails.Where(e => !domains.Any(d => e.EndsWith(d)));

var test2NotInTest1 = test2.Where(t2 => test1.Count(t1 => t2.Contains(t1))==0);

Faster version as per Tim's suggestion:

var test2NotInTest1 = test2.Where(t2 => !test1.Any(t1 => t2.Contains(t1)));

Tags:

C#

Linq