LINQ查询创建StackOverflow异常 [英] LINQ query creates a StackOverflow Exception

查看:63
本文介绍了LINQ查询创建StackOverflow异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是LINQ-to-SQL.

This is LINQ-to-SQL.

我正在尝试建立SiteCategories的层次关系,以查看有多少个级别.

I'm trying to walk up a hierarchical relationship of SiteCategories to see how many levels there are.

int numLevels = 1;

//I tried setting this to new[] { parentID }.AsQueryable(); 
//but linq didn't like it
IQueryable<int> nextBatchOfParents = _catalogdb.SiteCategories
                    .Where(c => c.SiteCategoryId == parentID)
                    .Select(c => c.SiteCategoryId);

while ((nextBatchOfParents = _catalogdb.SiteCategoryRelationships
         .Where(rel => nextBatchOfParents.Any(x => x == rel.ChildSiteCategoryId))
         .Select(rel => rel.ParentSiteCategoryId)).Any())
                ++numLevels;

不幸的是,循环的第一次迭代会导致StackOverflow异常.我猜想我可以通过尽快实现大多数/所有这些查询来逃避这个问题,但是我希望有更好的方法来解决这个问题.

Unfortunately the first iteration of the loop causes a StackOverflow exception. I'm guessing I could sneak my way out of this by materializing most/all of these queries sooner, but I'm hoping there's a better way to fix this.

推荐答案

似乎您正在内部调用nextbatchofparents.

It looks like you're calling nextbatchofparents within itself.

这篇关于LINQ查询创建StackOverflow异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆