Tuesday, May 22, 2012

LINQ2SQL Stack overflow

I am getting a stack overflow with the following code. I know what the problem is, that it executes all "GetAllPages" in the



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


before it adds the 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)
};
}


How can i do this, correctly?



UPDATE:
The reason behind the code is that, i have a tree structured menu, where one menu item can have 0 to many child items.
The language part can be skipped, but my site support multiple languages, and with the language parameters do i only want menu items there have a content of the given language.





No comments:

Post a Comment