【发布时间】:2014-09-02 09:55:58
【问题描述】:
我有以下课程:-
public class SiteMapSection
{
public string sectionUrl { get; set; }
public List<SiteMapSubSection> subSection { get; set; }
}
public class SiteMapSubSection
{
public string subSectionUrl { get; set; }
public List<SiteMapArticle> article { get; set; }
}
public class SiteMapArticle
{
public string url { get; set; }
}
我在列表中使用 SiteMapSection 类作为类型:-
List<SiteMapSection> siteMapSection = new List<SiteMapSection>();
现在,我正在尝试在“siteMapSection”列表中添加项目,如下所示:-
foreach (var section in sections)
{
.....
siteMapSection.Add(new SiteMapSection { sectionUrl = section.Url });
.....
foreach (var subsection in subsections)
{
.....
siteMapSection.Add(new SiteMapSubSection { ??stuck_here?? });
.....
var articles = GetNextArticles(0, subSectionId, true, false);
.....
foreach(var article in articles)
{
siteMapSection.Add(new SiteMapArticle { ??stuck_here?? });
}
}
}
如何遍历集合并在 List siteMapSection 中添加项目。
更新的代码,这也不起作用,我只看到添加了 siteMapSection.Add(sms) 项目,但其他嵌套仍然为空
List<SiteMapSection> siteMapSection = new List<SiteMapSection>();
SectionArticle sa = new SectionArticle();
foreach (BE.Section section in Sections.Find(websiteId, parentSectionId))
{
int sectionId = section.Id;
var sms = new SiteMapSection();
sms.sectionUrl = Sections.VirtualPath(section) + ".aspx";
var _subsections = new List<SiteMapSubSection>();
foreach (BE.Section subsection in Sections.Find(websiteId, sectionId))
{
int subSectionId = subsection.Id;
var smss = new SiteMapSubSection();
smss.subSectionUrl = Sections.VirtualPath(subsection) + ".aspx";
var articles = sa.GetArticlesForSection(websiteId, subSectionId, 10);
var _articles = new List<SiteMapArticle>();
foreach (var article in articles)
{
var sma = new SiteMapArticle();
sma.url = article.Code + ".aspx";
_articles.Add(sma);
}
_subsections.Add(smss);
}
siteMapSection.Add(sms);
}
【问题讨论】:
-
你能详细说明一下??stuck_here??。有什么问题?
-
@Lee 我想在 SiteMapSection 类型的列表中添加项目。第一个??stuck_here = subSectionUrl(在SiteMapSubSection 中),第二个stuck_here = url(在SiteMapArticle 类中)
标签: c# list class collections