当T模型本身具有List <t>作为属性</t> </t>时,在List <t>中分配值

时间:2014-09-03 06:44:38

标签: c# list class

我需要编码帮助。我有3个类SiteMapSection(保存sectionUrl和SiteMapSubSection列表),SiteMapSubSection(保存subSectionUrl和SiteMapArticle列表)和SiteMapArticle。这是类结构。

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; }
}

现在,我使用上面的类作为模型来分配值:

        var siteMapSection = new List<SiteMapSection>();
        DL.SectionArticle sa = new NewsFeed.BusinessTier.DataAccessLayer.SectionArticle();

        foreach (BE.Section section in BL.Sections.Find(websiteId, parentSectionId))
        {
            int sectionId = section.Id;
            siteMapSection.Add(new SiteMapSection { sectionUrl = BL.Sections.VirtualPath(section) + ".aspx" });

            Debug.WriteLine(siteMapSection[0].sectionUrl);

            foreach (BE.Section subsection in BL.Sections.Find(websiteId, sectionId))
            {
                int subSectionId = subsection.Id;
                var smss = new List<SiteMapSubSection>();
                smss.Add(new SiteMapSubSection { subSectionUrl = BL.Sections.VirtualPath(subsection) + ".aspx" });

                Debug.WriteLine(smss[0].subSectionUrl);

                var articles = sa.GetArticlesForSection(websiteId, subSectionId, 10);
                foreach (var article in articles)
                {
                    var sma = new List<SiteMapArticle>();
                    sma.Add(new SiteMapArticle { url = BL.Sections.VirtualPath(subsection) + "/" + article.Code + "-" + UrlEncoding.ArticleEncode(article.Headline) + ".aspx" });

                    Debug.WriteLine(sma[0].url);
                }
            }
        }

上面的代码只适用于Debug.WriteLine()的输出。但是,当我将鼠标悬停在&#34; var siteMapSection&#34;它显示了&#39; sectionUrl&#39;和&#39; subSection&#39;一片空白。这是因为我在foreach中的单独变量中添加了值。如何修改代码(当我将鼠标悬停在var siteMapSection上时):

    siteMapSection
      [0]sectionUrl = "abc.com"
         subSection
         [0]subSectionUrl = "ced.com"
            [0]url = "zya.com"
            [1]url = "sd.com"
            [2]url = "ab.in"
            ::::::::::::::::
         [1]subSectionUrl = "abd.org"
            [0]url = "yy.in"
            ::::::::::::::::
         :::::::::::::::::::
      [1]sectionUrl = "yep.com"
         subSection
         [0]subSectionUrl = "ced.com"
            [0]url = "zya.com"
            [1]url = "sd.com"
            [2]url = "ab.in"
            ::::::::::::::::
         [1]subSectionUrl = "abd.org"
            [0]url = "yy.in"
            ::::::::::::::::
         :::::::::::::::::::
    :::::::::::::::::::::::::::

请帮我解决这个问题。提前谢谢。

3 个答案:

答案 0 :(得分:1)

初始化变量sectionsubsection中的集合并直接添加到它们中。

            var siteMapSection = new List<SiteMapSection>();
            DL.SectionArticle sa = new NewsFeed.BusinessTier.DataAccessLayer.SectionArticle();

            foreach (BE.Section section in BL.Sections.Find(websiteId, parentSectionId))
            {
                int sectionId = section.Id;
                var mySection = new SiteMapSection();
                mySection.sectionUrl = BL.Sections.VirtualPath(section) + ".aspx";
                mySection.subSection = new List<SiteMapSubSection>();

                siteMapSection.Add(mySection);// NOTICE CHANGE HERE

                //Debug.WriteLine(siteMapSection[0].sectionUrl);

                foreach (BE.Section subsection in BL.Sections.Find(websiteId, sectionId))
                {
                    int subSectionId = subsection.Id;

                    var mySubSection = new SiteMapSubSection();
                    mySubSection.subSectionUrl = BL.Sections.VirtualPath(subsection) + ".aspx";
                    mySubSection.article = new List<SiteMapArticle>();
enter code here
                    mySection.subSection.Add(mySubSection);// NOTICE CHANGE HERE

                    //Debug.WriteLine(smss[0].subSectionUrl);

                    var articles = sa.GetArticlesForSection(websiteId, subSectionId, 10);
                    foreach (var article in articles)
                    {

                        mySubSection.article.Add(new SiteMapArticle { url = BL.Sections.VirtualPath(subsection) + "/" + article.Code + "-" + UrlEncoding.ArticleEncode(article.Headline) + ".aspx" });// NOTICE CHANGE HERE

                        //Debug.WriteLine(sma[0].url);
                    }
                }
            }

答案 1 :(得分:1)

试试这个:

var siteMapSection = new List<SiteMapSection>();
        DL.SectionArticle sa = new NewsFeed.BusinessTier.DataAccessLayer.SectionArticle();

        foreach (BE.Section section in BL.Sections.Find(websiteId, parentSectionId))
        {
            int sectionId = section.Id;
            var siteMap=new SiteMapSection { sectionUrl = BL.Sections.VirtualPath(section) + ".aspx" };


            Debug.WriteLine(siteMap.sectionUrl);
        var smss = new List<SiteMapSubSection>();
            foreach (BE.Section subsection in BL.Sections.Find(websiteId, sectionId))
            {
                int subSectionId = subsection.Id;
                var sms=new SiteMapSubSection { subSectionUrl = BL.Sections.VirtualPath(subsection) + ".aspx" };
        Debug.WriteLine(smss[0].subSectionUrl);                



        var sma = new List<SiteMapArticle>();
                var articles = sa.GetArticlesForSection(websiteId, subSectionId, 10);
                foreach (var article in articles)
                {
                    var sm= new SiteMapArticle { url = BL.Sections.VirtualPath(subsection) + "/" + article.Code + "-" +             UrlEncoding.ArticleEncode(article.Headline) + ".aspx" };  
                    sma.Add(sm);

                    Debug.WriteLine(sm.url);
                }
        sms.article=sma;
        smss.Add(sms);
            }
        siteMap.subSection=smss;
        siteMapSection.Add(siteMap);
        }

答案 2 :(得分:0)

在构造函数

中初始化subSection
public class SiteMapSection
{
   public string sectionUrl { get; set; }
   public List<SiteMapSubSection> subSection { get; set; }
   public SiteMapSection()
   {
        subSection    =new List<SiteMapSubSection>();
   }

 }