예제 #1
0
        public static CollectionComparisonResult <T> CompareTo <T>([CanBeNull] this IEnumerable <T> source,
                                                                   [CanBeNull] IEnumerable <T> destination,
                                                                   [NotNull] Func <T, T, bool> comparer)
        {
            comparer.RequiredNotNull("comparer");

            var result = new CollectionComparisonResult <T>();

            if (source == null && destination == null)
            {
                return(result);
            }

            if (source != null && destination == null)
            {
                result.OnlyInSource.AddRange(source);
                return(result);
            }

            if (source == null)
            {
                result.OnlyInDestination.AddRange(destination);
                return(result);
            }

            var source_list = source.ConvertToList();

            Debug.Assert(source_list != null, "source_list != null");

            var destination_list = destination.ConvertToList();

            Debug.Assert(destination_list != null, "destination_list != null");

            foreach (var s in source_list)
            {
                if (destination_list.Any(d => comparer(s, d)))
                {
                    result.SourceInBoth.Add(s);
                }
                else
                {
                    result.OnlyInSource.Add(s);
                }
            }

            foreach (var d in destination_list)
            {
                if (source_list.Any(s => comparer(s, d)))
                {
                    result.DestinationInBoth.Add(d);
                }
                else
                {
                    result.OnlyInDestination.Add(d);
                }
            }

            return(result);
        }
예제 #2
0
        public static CollectionComparisonResult <TItem> CompareTo <TItem, TKey>([CanBeNull] this IEnumerable <TItem> source,
                                                                                 [CanBeNull] IEnumerable <TItem> destination,
                                                                                 [NotNull] Func <TItem, TKey> key)
        {
            key.RequiredNotNull("key");

            var result = new CollectionComparisonResult <TItem>();

            if (source == null && destination == null)
            {
                return(result);
            }

            if (source != null && destination == null)
            {
                result.OnlyInSource.AddRange(source);
                return(result);
            }

            if (source == null)
            {
                result.OnlyInDestination.AddRange(destination);
                return(result);
            }

            var source_list = source.ConvertToList();

            Debug.Assert(source_list != null, "source_list != null");

            var destination_list = destination.ConvertToList();

            Debug.Assert(destination_list != null, "destination_list != null");

            var source_keys      = new HashSet <TKey>(source_list.Select(key));
            var destination_keys = new HashSet <TKey>(destination_list.Select(key));

            foreach (var s in source_list)
            {
                if (destination_keys.Contains(key(s)))
                {
                    result.SourceInBoth.Add(s);
                }
                else
                {
                    result.OnlyInSource.Add(s);
                }
            }

            foreach (var d in destination_list)
            {
                if (source_keys.Contains(key(d)))
                {
                    result.DestinationInBoth.Add(d);
                }
                else
                {
                    result.OnlyInDestination.Add(d);
                }
            }

            return(result);
        }