62,046
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Data;
using System.Text;
namespace ConsoleApplication1
{
public class Program
{
/// <summary>
/// 获取模拟数据
/// </summary>
private static DataTable GetContent()
{
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Pid", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Rows.Add(1, 0, "A");
dt.Rows.Add(2, 0, "B");
dt.Rows.Add(3, 1, "C");
dt.Rows.Add(4, 3, "D");
dt.Rows.Add(5, 2, "E");
dt.Rows.Add(6, 5, "F");
dt.Rows.Add(7, 3, "G");
return dt;
}
private static string Recursive(DataTable dt, DataRow row,out int id)
{
//查找是否有子节点
var rows = dt.Select("Pid=" + row["Id"]);
//如果有子节点,那么加上其全部的父节点
if (rows != null && rows.Length > 0)
{
foreach (var subrow in rows)
{
return row["Name"].ToString() + "==>" + Recursive(dt, subrow,out id);
}
}
id = Convert.ToInt32(row["Id"]);
return row["Name"].ToString();
}
static void Main(string[] args)
{
DataTable dt = GetContent();
int id = 0;
foreach (DataRow srow in dt.Select("Pid=0"))
{
Console.WriteLine("路径:"+Recursive(dt,srow,out id)+"\t最终子节点的Id:"+id);
}
}
}
}
class RecursionTestDemo
{
public int ID { get; set; }
public int PID { get; set; }
public string Name { get; set; }
public override string ToString()
{
return string.Format("ID:{0} PID:{1} Name:{2}", this.ID, this.PID, this.Name);
}
}
class RecursionTree
{
public RecursionTree()
{
this.Childrens = new List<RecursionTree>();
}
public int ID { get; set; }
public string Name { get; set; }
public List<RecursionTree> Childrens { get; set; }
public RecursionTree Parent { get; set; }
}
private static void TestRecursionData()
{
var list = new List<RecursionTestDemo>()
{
new RecursionTestDemo{ ID=1,PID=0, Name="A"},
new RecursionTestDemo{ ID=2,PID=0, Name="B"},
new RecursionTestDemo{ ID=3,PID=1, Name="C"},
new RecursionTestDemo{ ID=4,PID=3, Name="D"},
new RecursionTestDemo{ ID=5,PID=2, Name="E"},
new RecursionTestDemo{ ID=6,PID=5, Name="F"},
new RecursionTestDemo{ ID=7,PID=3, Name="G"},
new RecursionTestDemo{ ID=8,PID=7, Name="H"},
};
var trees = new List<RecursionTree>();
list.Where(x => x.PID == 0).ToList().ForEach(x =>
{
RecursionTree tree = new RecursionTree() { ID = x.ID, Name = x.Name };
RecursiveDate(list, tree);
trees.Add(tree);
ConsoleTreeDepth(tree);
});
}
static void ConsoleTreeDepth(RecursionTree tree)
{
if (tree.Childrens.Count > 0)
{
tree.Childrens.ForEach(x => { ConsoleTreeDepth(x); });
}
else
{
List<RecursionTree> treeDepth = new List<RecursionTree>() { tree };
while (true)
{
tree = tree.Parent;
treeDepth.Insert(0, tree);
if (tree.Parent == null) { break; }
}
Console.WriteLine("路径:" + string.Join("===>", treeDepth.Select(x => x.Name)) + "\t最终子节点的Id:" + treeDepth[treeDepth.Count - 1].ID);
}
}
private static void RecursiveDate(List<RecursionTestDemo> list, RecursionTree tree)
{
var tmpList = list.Where(x => x.PID == tree.ID);
foreach (var demo in tmpList)
{
RecursionTree tmpTree = new RecursionTree() { ID = demo.ID, Parent = tree, Name = demo.Name };
tree.Childrens.Add(tmpTree);
RecursiveDate(list, tmpTree);
}
}
static void Main(string[] args)
{
DataTable dt = GetContent();
int id = 0;
foreach (DataRow srow in dt.Select("Pid=0"))
{
Console.WriteLine("路径:"+Recursive(dt,srow,out id)+"\t最终子节点的Id:"+id);
}
}
单独靠这样肯定不行的……你这个代码都要改,如果要,我等下用List重写个,DataTable太不舒服了foreach (var subrow in rows)
{
return row["Name"].ToString() + "==>" + Recursive(dt, subrow,out id);
}
你这里找到第一个就出去了,当然没有后面的了
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testcsdn
{
class Program
{
public static List<Tables> Tablist = new List<Tables>();//在查询级别的相同项
public static StringBuilder result = new StringBuilder(); //返回的字符串
public static List<Tables> resultlist = new List<Tables>();//查询父节点下所有子节点的集合
/// <summary>
/// 获取模拟数据
/// </summary>
private static List<Tables> GetContent()
{
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Pid", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Rows.Add(1, 0, "A");
dt.Rows.Add(2, 0, "B");
dt.Rows.Add(3, 1, "C");
dt.Rows.Add(4, 3, "D");
dt.Rows.Add(5, 2, "E");
dt.Rows.Add(6, 5, "F");
dt.Rows.Add(7, 3, "G");
List<Tables> list = new List<Tables>();
foreach (DataRow item in dt.Rows)
{
Tables mod = new Tables();
mod.Id = (int)item["Id"];
mod.Pid = (int)item["Pid"];
mod.Name = item["Name"].ToString();
list.Add(mod);
}
return list ;
}
/// <summary>
/// 查询出顶级父节点下所有子节点
/// </summary>
/// <param name="list"></param>
/// <param name="Pid"></param>
private static void Recursive(List<Tables> list, int Pid)
{
foreach (var item in list)
{
if(item.Pid==Pid)
{
//result.Append(item.Name.ToString() + "==>");
if (resultlist.Where(w => w.Pid == item.Pid).Count() > 0)
{
Tablist.Add(resultlist.Where(w => w.Pid == item.Pid).First());
Tablist.Add(item);
}
resultlist.Add(item);
Recursive(list, item.Id);
}
}
}
static void Main(string[] args)
{
List<Tables> list = GetContent();
var pid = 0;//顶级父节点
var Flist =list.Where(w => w.Pid == pid);
foreach (var item in Flist)
{
resultlist = new List<Tables>();
Recursive(list, item.Id);
GetReturnPath(resultlist,item);
}
Console.ReadLine();
}
/// <summary>
/// 一级一级往下处理(包含处理同级中有相同级别)
/// </summary>
/// <param name="list"></param>
/// <param name="table">父节点的table</param>
public static void GetReturnPath(List<Tables> list,Tables table)
{
bool IsTrue = false; //是否有同级
var retrunlist = new List<Tables>(); //记录最后返回
if (Tablist.Count > 0)
{
retrunlist.Add(table); //顶级
retrunlist.Add(Tablist.First());
Tablist.Remove(Tablist.First());
foreach (var item in list)
{
if (retrunlist.Where(w => w.Pid == item.Pid).Count() <= 0)
{
retrunlist.Add(item);
//result.Append(item.Name.ToString() + "==>");
}
}
if(Tablist.Count>0) //还有同级没处理
{
IsTrue = true;
}
}
else
{
retrunlist.Add(table);
foreach (var item in list)
{
if (retrunlist.Where(w => w.Pid == item.Pid).ToList().Count <= 0)
{
retrunlist.Add(item);
//result.Append(item.Name.ToString() + "==>");
}
}
}
result = new StringBuilder();
var index = 0;
foreach (var item in retrunlist.OrderBy(o => o.Pid))
{
index++;
if (index == retrunlist.Count)
{
result.Append(item.Name.ToString());
result.Append("\t\t最终子节点的Id:" + item.Id);
}
else
{
result.Append(item.Name.ToString() + "==>");
}
}
Console.WriteLine(result);
if(IsTrue)
{
GetReturnPath(list, table);
}
}
}
public class Tables
{
public int Id { get; set; }
public int Pid { get; set; }
public string Name { get; set; }
}
}