现在有 unt_id unt_parent 两个列名
根据已知的 一个unt_id查询 他的下级
unt_parent 副id
无限下级最后结果想要的是 所有id的一个字符串用逗号分割

解决方案 »

  1.   

    http://blog.csdn.net/rubychen410/article/details/4282175
      

  2.   


            string uu = "";
            public void untids(string uid)
            {
                string uitis = "";
                using (DataBxDataContext bx = new DataBxDataContext())
                {
                    List<Units> ulist = bx.Units.Where(p => p.Unt_Parent == Convert.ToInt32(uid)).ToList();                if (ulist.Count > 0)
                    {
                        foreach (Units u in ulist)
                        {
                            untids( u.Unt_ID.ToString());
                        }
                    }
                    else
                    {
                        uu +=","+ uid;
                    }
    这个有问题啊
    得到的结果是 6,4,5 数据应该是
    23456啊
    2 一级单位 1
    3 二级单位1 2
    4 二级单位2 2
    5 二级单位3 2
    6 三级单位1 3
      

  3.   

    with cte as
    (
      select unt_id,unt_parent from tb where unt_id=@unt_id
      union all
      select a.unt_id,a.unt_parent from tb a join cte b on a.unt_parent=b.unt_id
    )select unt_id+',' from cte for xml path('')
      

  4.   

    用sql递归
    给你个参考
    WITH prgs(parent_key, parent_id, child_key, child_id) AS 
    (
        SELECT parent_key, parent_id, child_key, child_id
        FROM tablename
        WHERE 查询条件
         UNION ALL
        SELECT e.parent_key, e.parent_id, e.child_key, e.child_id
        FROM tablename e 
    inner join prgs a ON
    (a.child_key=e.parent_key AND a.child_id=e.parent_id )
    WHERE e.group_id=10 
    )
    select * from prgs
      

  5.   


            string unitids = "";
            public void untids(string uid)
            {
       
                using (DataBxDataContext bx = new DataBxDataContext())
                {
                    List<Units> ulist = bx.Units.Where(p => p.Unt_Parent == Convert.ToInt32(uid)).ToList();                if (ulist.Count > 0)
                    {
                        foreach (Units u in ulist)
                        {
                            
                            untids( u.Unt_ID.ToString());
                        }
                        unitids += "," + uid;
                    }
                    else
                    {
                        unitids += "," + uid;
                    }
                }
            }自己解决了
      

  6.   

    虚拟表不会玩啊。linq能玩虚拟表吗