Last active
December 20, 2015 12:39
-
-
Save afreeland/6133327 to your computer and use it in GitHub Desktop.
C#: Extension to Flatten Tree iterating through List of Tree's
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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