LINQ2SQL堆栈溢出

时间:2012-05-22 03:44:50

标签: asp.net asp.net-mvc-3 linq-to-sql

我使用以下代码获得堆栈溢出。我知道问题是什么,它在

中执行所有“GetAllPages”
           Children = new LazyList<Page>(from p in GetAllPages(language)
                                         where p.ParentPage == s.Id
                                         select p)

在添加p.ParentPage == s.Id

之前
private IQueryable<Page> GetAllPages(string language)
{
    return from s in context.Pages
           where (from c in GetAllContent()
                  where c.PageId == s.Id &&
                        c.Language.ToLower() == language.ToLower()
                  select c).Any()

           let contents = (from c in GetAllContent()
                           where c.PageId == s.Id
                           select c)
           select new Page()
           {
               Id = s.Id,
               SiteId = s.SiteId,
               Type = s.Type,
               Template = s.Template,
               ParentPage = s.ParentPage,
               Visible = s.Visible,
               Order = s.Order,

               Contents = contents.ToList(),
               Children = new LazyList<Page>(from p in GetAllPages(language)
                                             where p.ParentPage == s.Id
                                             select p)
           };
}

我怎么能正确地做到这一点?

更新: 代码背后的原因是,我有一个树形结构菜单,其中一个菜单项可以有0到多个子项。 语言部分可以跳过,但我的网站支持多种语言,并且使用语言参数我只想要菜单项,其中包含给定语言的内容。

1 个答案:

答案 0 :(得分:0)

从您调用此GetAllPages(language)的那一刻起,然后再次调用它而不更改language,您将获得100%的堆栈溢出。

你需要得到不同的东西,不一样!并且您唯一的参数是language,这不会改变。

private IQueryable<Page> GetAllPages(string language)
{
 ....
   Children = new LazyList<Page>(from p in GetAllPages(language) // <-- here you 
          // call him self again with the same parametre.
          // Your function is going to call him self asking the same think
          //  This is bring the stack overflow
          //  When you make call to the same function you need some how
          //   to get different results with some different parameter.