最近在学asp.net 2010 step by step,有一个问题一直没有搞清楚,还望你能帮我解答一下:
 遍历一个泛型数据所得的不同值如何加到新的一个泛型实例中??      protected void  (List<Category>  int rootCategoryId, Category currentCategory, int level)
        {
foreach (var category in CategoryManager.GetAllCategoriesByParentCategoryId(rootCategoryId))
             { 
                string categoryURL = SEOHelper.GetCategoryUrl(category); 
                string name = Server.HtmlEncode(category.LocalizedName); 
              
                 SiteNode n1 = new SiteNode();
                 n1.ID =Convert.ToString( category.CategoryId);
                 n1.ParentID = category.ParentCategoryId.ToString();
                 n1.Title = name;
                 n1.Url = categoryURL;
               
                  List<SiteNode> nodes = new List<SiteNode>(); //创建一个新实例。                     nodes.Add(n1); //==这里,每次循环执行完成后,只能保存最后一行内容。
               … …..
           是不是List<T> ,只能一个个item进行操作呢?
 还有更好的方法吗??

解决方案 »

  1.   

    List<SiteNode> nodes = new List<SiteNode>(); //创建一个新实例。
    foreach (var category in CategoryManager.GetAllCategoriesByParentCategoryId(rootCategoryId))
                 { 
                    string categoryURL = SEOHelper.GetCategoryUrl(category); 
                    string name = Server.HtmlEncode(category.LocalizedName); 
                  
                     SiteNode n1 = new SiteNode();
                     n1.ID =Convert.ToString( category.CategoryId);
                     n1.ParentID = category.ParentCategoryId.ToString();
                     n1.Title = name;
                     n1.Url = categoryURL;
                                        nodes.Add(n1); //==这里,每次循环执行完成后,只能保存最后一行内容。
                   … …..这样试试
      

  2.   

    这样更改后,也只能保存两行内容。ID ParentID Title               Url
    7          3       第四级菜单 http://localhost:620/7-.aspx表里总数有8行数据啊。不得其解??还有更好的方法吗》》??
      

  3.   

    谁来帮帮我,在调试过程序中好象在每次循环过后,list<T> 的值就没有了。ADD的方法不是加到最尾部吗,为什么会被重置了呢。
      

  4.   

    List<SiteNode> nodes = new List<SiteNode>(); //创建一个新实例。把这行放到foreach外面去
      

  5.   

                    SiteNode n1 = new SiteNode();
    这个是引用类型,你每次实例化都把前一个对象重新实例化了。在内存里面是同一个地址。所以你应该for(int i=0;i<CategoryManager.GetAllCategoriesByParentCategoryId(rootCategoryId).items.count;i++)             { 
                    string categoryURL = SEOHelper.GetCategoryUrl(category); 
                    string name = Server.HtmlEncode(category.LocalizedName); 
                  
                     SiteNode n+i.toString() = new SiteNode();
                     n1.ID =Convert.ToString( category.CategoryId);
                     n1.ParentID = category.ParentCategoryId.ToString();
                     n1.Title = name;
                     n1.Url = categoryURL;
                                        nodes.Add(n+i.toString()); //==这里,每次循环执行完成后,只能保存最后一行内容。
      

  6.   

    不能这样用for 吧,因为我要遍历一个泛型集合,从里中得到泛型集合中的属性和值啊。这样行不通吧。
      

  7.   

    第一,你不用for一样的。可以在外面定义一个int i=0;
    循环内执行完后,执行i+=1;第二,for循环怎么就不行了?你还是认真实践下再来说。
      

  8.   

    你好,jxyxhz!   我试了,确实不行。因为我不知如何来改(毕竟我不懂,我正在学)。如果用for 后,
    string categoryURL = SEOHelper.GetCategoryUrl(category);   // 这个(category)怎么办。
     
    如果我这样问是不是会更明白点呢?
       protected void CreateChildMenu(List<Category> breadCrumb, int rootCategoryId, Category currentCategory, int level)
            {
                int padding = level++; //加入一个自增,每循环一次+1         foreach (var category in CategoryManager.GetAllCategoriesByParentCategoryId(rootCategoryId))
                {
                    List<SiteNode> nodes = new List<SiteNode>();                SiteNode  n1 = new SiteNode(); // n1 我应该如何把Padding 的值赋给 n1                n1.CategoryId = Convert.ToString(category.CategoryId);  //n1随每次循环值都不同。
                    n1.ParentCategoryId= category.ParentCategoryId.ToString();
                    n1.LocalizedName= Server.HtmlEncode(category.LocalizedName);
                    n1.Url = SEOHelper.GetCategoryUrl(category);
                 
                     nodes.Add(n1);  //这个n1 怎样能让他每一次循环后自动变成循环后的值。比如:n2                
      

  9.   

    搞定了,应该有把 List<SiteNode> nodes = new List<SiteNode>();放在方法的外面,因为循环是整个方法在循环。而不是单从Foreach中循环。