private void RebuildTree() { var list = Loaded.ToDictionary(x => x.Id, x => x); foreach (var careerObject in list.Values) { careerObject.NextCareerObject = null; } foreach (var career in list.Values) { var previous = (from id in career.RequiredSeries where list.ContainsKey(id) let entry = list[id] where entry.NextCareerObject == null select entry).ToList(); if (!previous.Any()) { continue; } if (!career.RequiredAnySeries) { var p = previous[0]; foreach (var o in previous.Skip(1)) { p.NextCareerObject = o; } } foreach (var previousCareer in career.RequiredAnySeries ? previous : previous.TakeLast(1)) { previousCareer.NextCareerObject = career; } } }