public static List <dynamic> ListToTreeData <T>(List <T> source, string ID, string PID) where T : class, new() { Action <List <dynamic>, T, dynamic> AddItem = (parent, item, Recursive) => { var childrens = new List <dynamic>(); var thisitem = GenericHelper.GetDictionaryValues(item); source.FindAll(o => GenericHelper.GetValue(item, ID).Equals(GenericHelper.GetValue(o, PID))) .ForEach(subitem => { Recursive(childrens, subitem, Recursive); }); thisitem.Add("children", childrens); parent.Add(thisitem); }; var target = new List <dynamic>(); source.FindAll(m => { return(!source.Exists(n => GenericHelper.GetValue(n, ID).Equals(GenericHelper.GetValue(m, PID)))); }) .ForEach(item => AddItem(target, item, AddItem)); return(target); }