2012-05-22 2 views
0

다음 코드를 사용하여 스택 오버플로가 발생합니다. 나는 그것이 p.ParentPage ==를 추가하기 전에이LINQ2SQL 스택 오버플로

  Children = new LazyList<Page>(from p in GetAllPages(language) 
             where p.ParentPage == s.Id 
             select p) 

에있는 모든 "GetAllPages"를 실행하는 문제가 무엇인지 알고 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 개 이상의 하위 항목을 포함 할 수있는 트리 구조 메뉴가 있기 때문입니다. 언어 부분은 건너 뛸 수 있지만 내 사이트는 여러 언어를 지원하며 언어 매개 변수를 사용하면 메뉴 항목 만 원한다면 주어진 언어의 콘텐츠가 있어야합니다.

+0

나는이 문제를 이해할 수 있지만 코드의 배경을 이해하지 못한다. 코드에서 수행하려는 작업과 재귀가 필요한 이유를 설명해 주시겠습니까? – peter

+0

방금 ​​더 많은 정보로 내 게시물을 업데이트했습니다. – Androme

+0

parentid를 인수로 사용하는 복제 기능을 작성하여 임시 수정을했습니다. 하지만 나는 데이터를 중복하지 않기를 원합니다. D – Androme

답변

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.