/// <summary> /// Takes a flat list of MyPurdueSection objects and groups them together based on their LinkSelf and LinkOther values. /// </summary> /// <param name="sections">Flat list of MyPurdueSection objects</param> /// <returns>Groups of sections to be processed into "classes"</returns> private List <List <MyPurdueSection> > GroupSections(List <MyPurdueSection> sections) { var sectionsByLinks = new Dictionary <Tuple <string, string>, LinkSections>(); var independentSections = new List <MyPurdueSection>(); foreach (var section in sections) { if (section.LinkSelf == "" && section.LinkOther == "") { independentSections.Add(section); continue; } var linkSelfTuple = new Tuple <string, string>(section.LinkSelf, section.Number); var linkOtherTuple = new Tuple <string, string>(section.LinkOther, section.Number); LinkSections linkSelfGroups = null; sectionsByLinks.TryGetValue(linkSelfTuple, out linkSelfGroups); LinkSections linkOtherGroups = null; sectionsByLinks.TryGetValue(linkOtherTuple, out linkOtherGroups); if (linkSelfGroups == null && linkOtherGroups == null) { var group = new LinkSections(section.LinkSelf, section.LinkOther, section); sectionsByLinks[linkSelfTuple] = group; sectionsByLinks[linkOtherTuple] = group; } else if (linkSelfGroups != null && linkOtherGroups == null) { linkSelfGroups.Add(section); sectionsByLinks[linkOtherTuple] = linkSelfGroups; } else if (linkSelfGroups == null && linkOtherGroups != null) { linkOtherGroups.Add(section); sectionsByLinks[linkSelfTuple] = linkOtherGroups; } else if (linkSelfGroups != null && linkOtherGroups != null) { if (linkSelfGroups != linkOtherGroups) { linkSelfGroups.Absorb(linkOtherGroups); foreach (var l in linkSelfGroups.Links) { sectionsByLinks[new Tuple <string, string>(l, section.Number)] = linkSelfGroups; } } linkSelfGroups.Add(section); } } var sectionGroups = sectionsByLinks.Values.Distinct().Select(s => s.Sections).ToList(); sectionGroups.AddRange(independentSections.Select(x => new List <MyPurdueSection>() { x })); return(sectionGroups); }
public void Absorb(LinkSections other) { foreach (var l in other.Links) { if (!Links.Contains(l)) { Links.Add(l); } } Sections.AddRange(other.Sections); }