private string GetMenuProject(List<MenuLeft> menus) { var parents = menus.Where(t => t.ParentId == null).OrderBy(t => t.Position); var parentses = new List<Parents>(); foreach (var parent in parents.Where(parent => parent != null)) { var menuParent = new Parents { Id = parent.Id, Name = parent.Name }; var childs = menus.Where(t => t.ParentId != null && t.ParentId == parent.Id).ToList(); if(childs.Count == 0) { menuParent.Childs = new List<Childs>(); } else { foreach (var menuChild in childs.Select(child => new Childs { Id = child.Id, Name = child.Name })) { menuParent.Childs.Add(menuChild); } } parentses.Add(menuParent); } object json = new { parent = parentses }; var res = Utils.ConvertToJsonString(json); return res; }
private List<Parents> GetMenuParents(List<MenuLeft> menus) { var parentses = new List<Parents>(); foreach (var parent in menus.Where(parent => parent != null)) { var menuParent = new Parents { Id = parent.Id, Name = parent.Name }; var childs = menus.Where(t => t.ParentId != null && t.ParentId == parent.Id).ToList(); if (childs.Count == 0) { menuParent.Childs = new List<Childs>(); } else { foreach (var menuChild in childs.Select(child => new Childs{ Id = child.Id, Name = child.Name})) { menuParent.Childs.Add(menuChild); } } parentses.Add(menuParent); } return parentses; }