private static ModuleTreeNode GetSubItem(IEnumerable<ModuleTreeNode> source, ModuleTreeNode parentNode, Guid? currentId) { if (source == null || parentNode == null) { return null; } var list = source.Where(x => x.ParentId == parentNode.Id).OrderBy(x => x.Order); foreach (var item in list) { var json = JsonConvert.SerializeObject(item); var child = JsonConvert.DeserializeObject<ModuleTreeNode>(json); child.IsActived = child.Id == currentId; parentNode.Children.Add(GetSubItem(source, child, currentId)); if (child.Children.Any(x => x.IsActived)) { child.IsActived = true; } } return parentNode; }
public async Task<ModuleTreeNode> GetModuleTree(NullableIdInput<Guid> input) { var query = _repository.GetAll(); var list = await query.ToListAsync(); var mappedList = list.MapTo<List<ModuleTreeNode>>(); var root = new ModuleTreeNode { ModuleCode = "Root", DisplayName = "Root", ParentId = null, Id = null }; return GetSubItem(mappedList, root, input.Id); }