Flatten objects tree structure:
protected IEnumerable<T> Flatten<T>(IEnumerable<T> elements, Func<T, IEnumerable<T>> childrenFunc) { List<T> result = new List<T>(); result.AddRange(elements); IEnumerable<T> children = elements.SelectMany(childrenFunc); if (children.Count() == 0) { return result; } result.AddRange(this.Flatten<T>(children, childrenFunc)); return result; }
No comments:
Post a Comment