code_junkie

Determine if a sequence contains all elements of another sequence using Linq

Nov 13th, 2011
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.49 KB | None | 0 0
  1. var subset = new[] { 2, 4, 6, 8 };
  2.  
  3. var superset = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  4.  
  5. superset.Intersect(subset).Count() == subset.Count()
  6.  
  7. bool contained = !subset.Except(superset).Any();
  8.  
  9. HashSet<int> hashSet = new HashSet<int>(superset);
  10. bool contained = subset.All(i => hashSet.Contains(i));
  11.  
  12. subset.Except(superset).Count() == 0
  13.  
  14. public static bool ContainsAll<T>(this IEnumerable<T> source, IEnumerable<T> values)
  15. {
  16. return values.All(value => source.Contains(value));
  17. }
Add Comment
Please, Sign In to add comment