最近在学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进行操作呢?
还有更好的方法吗??
遍历一个泛型数据所得的不同值如何加到新的一个泛型实例中?? 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进行操作呢?
还有更好的方法吗??
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); //==这里,每次循环执行完成后,只能保存最后一行内容。
… …..这样试试
7 3 第四级菜单 http://localhost:620/7-.aspx表里总数有8行数据啊。不得其解??还有更好的方法吗》》??
这个是引用类型,你每次实例化都把前一个对象重新实例化了。在内存里面是同一个地址。所以你应该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()); //==这里,每次循环执行完成后,只能保存最后一行内容。
循环内执行完后,执行i+=1;第二,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