数据结构如下
id name parentid m p
1  中国 0 1 2
2  广东 1 2 3
3  广州 2 1 2
4  天河 3 3 4
5  上社 4 5 7
......
想统计出广东下m的集合 

解决方案 »

  1.   

    不需要递归List<m> ms = new List<m>();
    Queue<id> ids = new Queue<id>();
    ids.Add(2);
    while (ids.Count > 0)
    {
      List<record> children = FindChildren(ids.Dequeue());
      for (int i = 0; i < children.Count; ++i)
      {
        ms.Add(children[i].m);
        ids.Enqueue(children[i].id);
      }
    }根据排序规则什么的可以做一些优化
      

  2.   

    就是表里面了,
    没明白意思说清楚:
    id name  parentid m p 
    1  中国   0        1 2 
    2  广东   1        2 3 
    3  广州   2        1 2 
    4  天河   3        3 4 
    5  上社   4        5 7 得到广东下m的集合就是id为3,4,5的m值的和
      

  3.   

            static void Main(string[] args)
            {
                SqlConnection sc = new SqlConnection("连接字符串");
                SqlDataAdapter sda = new SqlDataAdapter("select * from 表名", sc);
                sc.Open();
                DataSet ds = new DataSet();
                sda.Fill(ds);
                List<int> list = GetmListFromName(ds, "广东");
                int count = 0;//count就是所求啦
                foreach (int i in list)
                    count += i;
                Console.WriteLine(count);
                sc.Close();
            }        static List<int> GetmListFromName(DataSet ds, string name)
            {
                if (ds.Tables[0].Rows.Count <= 0)
                    return null;
                List<int> list = new List<int>();
                string expression = "name='" + name + "'";
                DataRow[] foundRows = ds.Tables[0].Select(expression);
                foreach (DataRow dr in foundRows)
                {
                    int id = Convert.ToInt32(dr["id"]);
                    list.AddRange(GetChildList(ds, id));
                }
                return list;
            }        static List<int> GetChildList(DataSet ds, int parentid)
            {
                List<int> list = new List<int>();
                string expression = " parentid='" + parentid + "'";
                DataRow[] foundRows = ds.Tables[0].Select(expression);
                foreach (DataRow dr in foundRows)
                {
                    int id = Convert.ToInt32(dr["id"]);
                    int m = Convert.ToInt32(dr["m"]);
                    list.Add(m);
                    list.AddRange(GetChildList(ds,id));
                }
                return list;
            }
      

  4.   


    int sum =0;
    int id=1 ;privite void getsum(int id, int value)
    {
        id=id+1;
        if(id>=3)
        { 
           sum = value+sum;
           value = //somemethod get value
           getsum(id,value);
        }
    }getsum(1,0);result = sum
      

  5.   

    id name  parentid m p  
    1  中国   0        1 2  
    2  广东   1        2 3  
    3  广州   2        1 2  
    4  天河   3        3 4  
    5  上社   4        5 7  
    楼上的都是些神仙,看看他这个字段,parentid,乱的,能统计出来???
    id name  parentid m p  
    1  中国   0        1 2  
    2  广东   1        2 3  
    3  广州   2        1 2  
    4  天河   2        3 4  
    5  上社   2        5 7 
      

  6.   

    create table tb(id int,name varchar(50),parentid int,m int,p int)
    insert into tb select 1,'中国',0,1,2
    insert into tb select 2,'广东',1,2,3
    insert into tb select 3,'广州',2,1,2
    insert into tb select 4,'天河',3,3,4
    insert into tb select 5,'上社',4,5,7drop function f_cidcreate function f_Cid(@ID char(3))
    returns @t_level table(ID char(3),Level int)
    AS
    BEGIN
    DECLARE @Level int
    SET @Level=1
    INSERT @t_Level SELECT @ID,@Level
    WHILE @@ROWCOUNT>0
    BEGIN
    SET @Level=@Level+1
    INSERT @t_Level SELECT a.ID,@Level
    FROM tb a,@t_Level b
    WHERE a.parentid=b.ID
    AND b.Level=@Level-1
    END
    RETURN
    ENDSELECT a.*
    FROM tb a,f_Cid(2) b
    WHERE a.ID=b.IDid name parentid m p
    2 广东 1 2 3
    3 广州 2 1 2
    4 天河 3 3 4
    5 上社 4 5 73,2,4,7