using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; namespace NFine.Code { public static class TreeQuery { public static List TreeWhere(this List entityList, Predicate condition, string keyValue = "F_Id", string parentId = "F_ParentId") where T : class { List locateList = entityList.FindAll(condition); var parameter = Expression.Parameter(typeof(T), "t"); List treeList = new List(); foreach (T entity in locateList) { treeList.Add(entity); string pId = entity.GetType().GetProperty(parentId).GetValue(entity, null).ToString(); while (true) { if (string.IsNullOrEmpty(pId) && pId == "0") { break; } Predicate upLambda = (Expression.Equal(parameter.Property(keyValue), Expression.Constant(pId))).ToLambda>(parameter).Compile(); T upRecord = entityList.Find(upLambda); if (upRecord != null) { treeList.Add(upRecord); pId = upRecord.GetType().GetProperty(parentId).GetValue(upRecord, null).ToString(); } else { break; } } } return treeList.Distinct().ToList(); } } }