private static IEnumerable <T> DescendantsCore <T, K>(IEnumerable <T> source, Func <T, K> keySelector, Func <T, K> parentKeySelector, Equality <K> equality, K startKey, bool descendantsOnly = false, int?levelsCount = null) where T : class where K : class { PwrList <T> list = new PwrList <T>(); PwrFrameListView <T> view = new PwrFrameListView <T>(list); int priorCount = list.Count; if (descendantsOnly) { list.AddRange(source.Where(t => equality(parentKeySelector(t), startKey))); } else if (startKey != null) { list.AddRange(source.Where(t => equality(keySelector(t), startKey))); } int takeCount = list.Count - priorCount; for (int passCount = 1; (!levelsCount.HasValue || levelsCount.Value > passCount) && takeCount > 0; passCount++) { view.FrameOffset = priorCount; view.FrameSize = takeCount; priorCount = list.Count; list.AddRange(source.Where(t1 => parentKeySelector(t1) != null && view.Any(t2 => equality(keySelector(t2), parentKeySelector(t1))))); takeCount = list.Count - priorCount; } return(list.AsEnumerable()); }
private static IEnumerable <T> AscendantsCore <T, K>(IEnumerable <T> source, Func <T, K> keySelector, Func <T, K> parentKeySelector, Equality <K> equality, K startKey, bool ascendantsOnly = false, int?levelsCount = null) where T : class where K : class { PwrList <T> list = new PwrList <T>(); T item = source.SingleOrDefault(t => equality(keySelector(t), startKey)); if (item != null && ascendantsOnly) { K parentKey = parentKeySelector(item); item = parentKey != null?source.SingleOrDefault(t => equality(keySelector(t), parentKey)) : null; } if (item != null) { list.Add(item); } for (int passCount = 1; (!levelsCount.HasValue || levelsCount.Value > passCount) && item != null; passCount++) { K parentKey = parentKeySelector(item); item = parentKey != null?source.SingleOrDefault(t => equality(keySelector(t), parentKey)) : null; if (item != null) { list.Add(item); } } return(list.AsEnumerable()); }