Example #1
0
        public static MergeComparer <T> Create <TKey>(Expression <Func <T, TKey> > keyAccessor, bool ignoreKey)
        {
            var matchAlgorithm = new MatchAlgorithm <T>();
            var mergeAlgorithm = new CompareAlgorithm <T, TKey>(keyAccessor, ignoreKey);

            return(new MergeComparer <T>(matchAlgorithm, mergeAlgorithm));
        }
Example #2
0
        private IEnumerable <MergeMatch> CompareInternal(IEnumerable <T> compareSource, IEnumerable <T> compareDestination)
        {
            var sources      = compareSource.Select((instance, id) => new MergeWrapper(id, instance)).ToArray();
            var destinations = compareDestination.Select((instance, id) => new MergeWrapper(id, instance)).ToArray();

            var matches             = new List <MergeMatch>();
            var matchedSources      = new HashSet <int>();
            var matchedDestinations = new HashSet <int>();

            var scores = from s in sources
                         from d in destinations
                         let score = MatchAlgorithm.CalclateMatchIndex(s.Instance, d.Instance)
                                     orderby score descending
                                     select new MergeScore(s, d, score);

            foreach (var score in scores)
            {
                // not a match
                if (score.Score == 0)
                {
                    continue;
                }

                // already matched
                if (matchedDestinations.Contains(score.Destination.Id) || matchedSources.Contains(score.Source.Id))
                {
                    continue;
                }

                matches.Add(new MergeMatch(score));
                matchedSources.Add(score.Source.Id);
                matchedDestinations.Add(score.Destination.Id);
            }

            foreach (var source in sources.Where(s => !matchedSources.Contains(s.Id)))
            {
                matches.Add(new MergeMatch(source.Instance, null));
            }

            foreach (var destination in destinations.Where(d => !matchedDestinations.Contains(d.Id)))
            {
                matches.Add(new MergeMatch(null, destination.Instance));
            }

            return(matches);
        }