示例#1
0
        /// <summary>
        /// Compares two sequences and returns the added or removed items.
        /// Use this when T doesn't implement IEquatable
        /// </summary>
        /// <typeparam name="T">Type</typeparam>
        /// <param name="fresh">Recent sequence</param>
        /// <param name="old">Older sequence used as base of comparison</param>
        /// <param name="match">function to check equality</param>
        /// <returns></returns>
        public static IModifiedSet <T> Compare <T>(this IEnumerable <T> fresh, IEnumerable <T> old, Func <T, T, bool> match)
        {
            if (fresh == null)
            {
                throw new ArgumentNullException("fresh");
            }
            if (old == null)
            {
                throw new ArgumentNullException("old");
            }
            if (match == null)
            {
                throw new ArgumentNullException("match");
            }
            var mods = new ModifiedSet <T>();

            foreach (var item in old)
            {
                if (!fresh.Any(d => match(d, item)))
                {
                    mods.RemovedItem(item);
                }
            }

            foreach (var item in fresh)
            {
                if (!old.Any(d => match(d, item)))
                {
                    mods.AddedItem(item);
                }
            }
            return(mods);
        }
示例#2
0
        /// <summary>
        /// Compares two sequences and returns the added or removed items.
        /// </summary>
        /// <typeparam name="T">Implements IEquatable</typeparam>
        /// <param name="fresh">Recent sequence</param>
        /// <param name="old">Older sequence used as base of comparison</param>
        /// <returns></returns>
        public static IModifiedSet <T> Compare <T>(this IEnumerable <T> fresh, IEnumerable <T> old) where T : IEquatable <T>
        {
            if (fresh == null)
            {
                throw new ArgumentNullException("fresh");
            }
            if (old == null)
            {
                throw new ArgumentNullException("old");
            }
            var mods = new ModifiedSet <T>();

            foreach (var item in old)
            {
                if (!fresh.Contains(item))
                {
                    mods.RemovedItem(item);
                }
            }

            foreach (var item in fresh)
            {
                if (!old.Contains(item))
                {
                    mods.AddedItem(item);
                }
            }
            return(mods);
        }