linq-Except


源码:

        public static IEnumerable Except(this IEnumerable first, IEnumerable second)
        {
            if (first == null) throw Error.ArgumentNull("first");
            if (second == null) throw Error.ArgumentNull("second");
            return ExceptIterator(first, second, null);
        }
 
        public static IEnumerable Except(this IEnumerable first, IEnumerable second, IEqualityComparer comparer)
        {
            if (first == null) throw Error.ArgumentNull("first");
            if (second == null) throw Error.ArgumentNull("second");
            return ExceptIterator(first, second, comparer);
        }
 
        static IEnumerable ExceptIterator(IEnumerable first, IEnumerable second, IEqualityComparer comparer) {
            Set set = new Set(comparer);
            foreach (TSource element in second) set.Add(element);
            foreach (TSource element in first)
                if (set.Add(element)) yield return element;
        }