Checking If All The Items In List Occur In Another List Using Linq
I am stuck with a problem here. I am trying to compare items in a list to another list with much more items using linq. For example: list 1: 10,15,20 list 2: 10,13,14,15,20,30,45,5
Solution 1:
How about:
if (!list1.Except(list2).Any())
That's about the simplest approach I can think of. You could explicitly create sets etc if you want:
HashSet<int> set2 = newHashSet<int>(list2);
if (!list1.Any(x => set2.Contains(x)))
but I'd expect that to pretty much be the implementation of Except anyway.
Solution 2:
This should be what you want:
!list1.Except(list2).Any()
Solution 3:
var result = list1.All(i => list2.Any(i2 => i2 == i));
Post a Comment for "Checking If All The Items In List Occur In Another List Using Linq"