遍历集合并在List <t> </t>中添加项目

时间:2014-09-02 09:55:58

标签: c# list class collections

我有以下课程: -

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类作为列表中的Type: -

List<SiteMapSection> siteMapSection = new List<SiteMapSection>();

现在,我试图在&#39; siteMapSection&#39;中添加项目。列表,如下所示: -

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 siteSection中添加项目。

更新了代码,这也行不通,我看到只添加了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);
        }

2 个答案:

答案 0 :(得分:0)

我刚刚意识到您正在尝试向List<SiteMapSection>添加不同的类型。您不能将不同类型添加到通用列表。创建列表时,您要定义列表中允许的类型,以及尝试添加不同类型的位置。

您需要更改

siteMapSection.Add(new SiteMapSubSection { ??stuck_here?? });

siteMapSection.Add(new SiteMapSection { ??stuck_here?? });

如果您提供更多背景信息,我们可以为您提供更好的方法。

希望这有帮助。

答案 1 :(得分:-1)

foreach (var section in sections)
{
    .....
    var sms = new SiteMapSection { sectionUrl = section.Url };
    sms.subSection = new List<SiteMapSubSection>();
    .....
    foreach (var subsection in subsections)
    {
        .....
        var smss = new new SiteMapSubSection { subsection }
        ssms.article = new List<SiteMapArticle>();
        .....
        var articles = GetNextArticles(0, subSectionId, true, false);
        .....
        foreach(var article in articles)
        {
            smss.article.Add(new SiteMapArticle { article });
        }
        sms.subSection.Add(ssms);

    }
    siteMapSection.Add(sms);

}