Skip to content

Instantly share code, notes, and snippets.

@afreeland
Last active December 20, 2015 12:39
Show Gist options
  • Save afreeland/6133327 to your computer and use it in GitHub Desktop.
Save afreeland/6133327 to your computer and use it in GitHub Desktop.
C#: Extension to Flatten Tree iterating through List of Tree's
public static IEnumerable<T> Flatten<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> childrenSelector)
{
foreach (var item in source)
{
yield return item;
foreach (var child in childrenSelector(item).Flatten(childrenSelector))
{
yield return child;
}
}
}
var _COA = GetObject<Account_ChartOfAccounts>(API.Get(API.Accounting.Actions.ChartOfAccounts, AccountID.ToString()), DataFormat.json, false, false, 100);
Dictionary<long, string> list = new Dictionary<long, string>();
foreach (var coa in _COA.Flatten(x => x.SubAccounts))
{
list.Add(coa.ID, coa.Name);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment