private HashSet <ContactInfoForLinking> LinkWithContacts(ContactInfoForLinking contactBeingSaved, IEnumerable <ContactInfoForLinking> otherContacts, int maxLinkCount, out bool linkedToDirectory) { linkedToDirectory = false; HashSet <ContactInfoForLinking> hashSet = new HashSet <ContactInfoForLinking>(ContactInfoForLinkingComparerByItemId.Instance); PersonId personId = contactBeingSaved.PersonId; HashSet <PersonId> hashSet2 = new HashSet <PersonId>(); ContactInfoForLinkingFromDirectory contactInfoForLinkingFromDirectory = null; if (contactBeingSaved.GALLinkState != GALLinkState.NotAllowed && contactBeingSaved.GALLinkID == null) { this.TryFindDirectoryMatch(contactBeingSaved, out contactInfoForLinkingFromDirectory); } foreach (ContactInfoForLinking contactInfoForLinking in otherContacts) { if (!this.CanContinueProcessingContacts()) { break; } if (!contactInfoForLinking.PersonId.Equals(personId) && !ContactInfoForLinkingComparerByItemId.Instance.Equals(contactInfoForLinking, contactBeingSaved) && !hashSet2.Contains(contactInfoForLinking.PersonId)) { ContactLinkingOperation contactLinkingOperation = AutomaticLinkCriteria.CanLink(contactBeingSaved, contactInfoForLinking); if (contactInfoForLinkingFromDirectory != null && contactInfoForLinking.GALLinkState == GALLinkState.Linked && contactInfoForLinking.GALLinkID == contactInfoForLinkingFromDirectory.GALLinkID) { if (contactLinkingOperation == ContactLinkingOperation.None) { contactLinkingOperation = ContactLinkingOperation.AutoLinkViaGalLinkId; } contactInfoForLinkingFromDirectory = null; } if (AutomaticLink.ShouldLink(contactLinkingOperation)) { hashSet2.Add(contactInfoForLinking.PersonId); HashSet <ContactInfoForLinking> hashSet3 = new HashSet <ContactInfoForLinking>(this.contactStoreForContactLinking.GetPersonContacts(contactInfoForLinking.PersonId)); if (hashSet3.Count + hashSet.Count <= maxLinkCount) { hashSet.UnionWith(hashSet3); this.LinkTwoContacts(contactLinkingOperation, contactBeingSaved, contactInfoForLinking); if (hashSet.Count == maxLinkCount) { string text = string.Format(CultureInfo.InvariantCulture, "Stopping linking iteration for PersonID {0} because we have reached maximum number of contacts for the persona.", new object[] { contactBeingSaved.PersonId }); ContactLink.Tracer.TraceDebug <string>((long)this.GetHashCode(), "AutomaticLinking.LinkWithContacts: {0}", text); base.LogEvent(new SchemaBasedLogEvent <ContactLinkingLogSchema.Warning> { { ContactLinkingLogSchema.Warning.Context, text } }); break; } } else { ContactLink.Tracer.TraceDebug <PersonId, VersionedId>((long)this.GetHashCode(), "AutomaticLinking.LinkWithContacts: Skipping link with PersonID {0} for item {1} because we would be over the maximum number of contacts for the persona.", contactInfoForLinking.PersonId, contactBeingSaved.ItemId); this.LogSkippedContactLinking(contactBeingSaved, hashSet.Count, maxLinkCount, contactInfoForLinking, hashSet3.Count); } } } } if (contactInfoForLinkingFromDirectory != null && contactBeingSaved.GALLinkState != GALLinkState.Linked) { this.LogGALLinkOperation(contactBeingSaved, contactInfoForLinkingFromDirectory); contactBeingSaved.SetGALLink(contactInfoForLinkingFromDirectory); linkedToDirectory = true; } return(hashSet); }