public string createTree(DataTable dt) { //DataTable转换为IList<实体类>,得到要转化的树形结构数据 IList <Node> list = ModelConvertHelper <Node> .ConvertToIList(dt); List <Node> lists = new List <Node>(); //查询最外层 List <Node> roots = list.Where(c => c.pid == 0).OrderBy(c => c.order).ToList(); if (roots == null) { return(lists.ToString()); } lists.AddRange(roots); while (true) { if (roots == null || roots.Count == 0) { break; } Node[] cachNodes = roots.ToArray(); roots.Clear(); for (int i = 0; i < cachNodes.Length; i++) { Node pNode = cachNodes[i]; int pIdNode = pNode.id; // List <Node> subs = list.Where(c => c.pid == pIdNode).OrderBy(c => c.order).ToList(); if (subs != null) { pNode.children = subs; roots.AddRange(subs); } } } string res = JsonConvert.SerializeObject(lists); return(res); }