CSharp examples for System.Collections.Generic:ICollection
Checks whether a collection contains all the elements of another.
using System.Linq; using System.Collections.ObjectModel; using System.Collections.Generic; using System;//w w w . j a v a2 s . c om public class Main{ /// <summary> /// Checks whether a collection contains all the elements of another. /// </summary> /// <typeparam name="T"> /// The type of item in the collection. /// </typeparam> /// <param name="collection1"> /// The collection to compare. /// </param> /// <param name="collection2"> /// The collection to compare with. /// </param> /// <param name="comparer"> /// The comparer. /// </param> /// <returns> /// Returns true if contains all; else false. /// </returns> public static bool ContainsAll<T>( this IEnumerable<T> collection1, IEnumerable<T> collection2, IEqualityComparer<T> comparer) { var container = new Dictionary<T, int>(comparer); foreach (var item in collection1) { if (container.ContainsKey(item)) { container[item]++; } else { container.Add(item, 1); } } foreach (var item in collection2) { if (container.ContainsKey(item)) { container[item]--; } else { return false; } } return container.Values.All(c => c == 0); } }