StackOverflowException caused by a linq query?

The problem might be this: countries have subregions and subregions have countries. I don't know how you implement the lazy list, but that might keep calling GetCountries and then GetSubRegions and so on. To find that out, I would launch the debugger en set breakpoints on the GetCountries and GetSubRegions method headers I tried similar patterns with LinqToSql, but it's hard to make bidirectional navigation work without affecting the performance to much.

That's one of the reasons I'm using N.

The problem might be this: countries have subregions and subregions have countries. I don't know how you implement the lazy list, but that might keep calling GetCountries and then GetSubRegions and so on. To find that out, I would launch the debugger en set breakpoints on the GetCountries and GetSubRegions method headers.

I tried similar patterns with LinqToSql, but it's hard to make bidirectional navigation work without affecting the performance to much. That's one of the reasons I'm using N.

Hai! I think your models are recursively calling a method unintentionally, which results in the stack overflow. Like, for instance, your Subregion object is trying to get Country objects, which in turn have to get Subregions.

Anyhow, it always helps to check the stack in a StackOverflow exception. If you see a property being accessed over and over, its most likely because you're doing something like this: public object MyProperty { set { MyProperty = value; }} Its easier to spot situations like yours, where method A calls method B which calls method A, because you can see the same methods showing up two or more times in the call stack.

To answer your edited question, namely: "linking objects to each other without triggering infinite loops": Assuming you've got some sort of relation where both sides need to know about the other... get hold of all the relevant entities in both sides, then link them together, rather than trying to make the fetch of one side automatically fetch the other. Or just make one side fetch the other, and then fix up the remaining one. So in your case, the options would be: Option 1: Fetch all countries (leaving Subregions blank) Fetch all Subregions (leaving Countries blank) For each Subregion, look through the list of Countries and add the Subregion to the Country and the Country to the Subregion Option 2: Fetch all countries (leaving Subregions blank) Fetch all Subregions, setting Subregion.

Countries via the countries list fetched above For each subregion, go through all its countries and add it to that country (Or reverse country and subregion) They're basically equialent answers, it just changes when you do some of the linking.

This will be very slow when you have a large database. Can you do this with lazy loading enabled? – Paco Oct 26 '08 at 20:12 Why would it be particularly slow?

You're only loading the same amount of data you'd be loading anyway, and then "linking" the relevant bits in memory. Obviously a hashtable will make finding things quicker than the simpler O(N * M) approach described in the first option, but I wanted to stay simple – Jon Skeet Oct 26 '08 at 21:27 Because you're not loading the same amount of data you might need to load. There is a many to many relationship between subregions and countries, so left outer joins or multiple queries will be needed.

These are expensive. – Paco Oct 26 '08 at 21:45 Not enough chars in commments. You will load all subregions with for each a different collection of countries and with for each country a different collection of subregions.

That will be a lot of calls to the database when you just want to display the name of one country. – Paco Oct 26 '08 at 21:47 A lazy loaded collection is only loaded when you access it. So GetCountries().

WithId(1) will make 1 call to the database and GetCountries(). WithId(1). SubRegions will make 2 calls.

Your version will make the maximum amount of db calls in every situation or I'm I misunderstanding somthing? – Paco Oct 26 '08 at 21:49.

Stackoverflow.com/.../stackoverflowexcep... 26, 2008 – edit #2: Question solved halfways. Stackoverflow.com/.../using-navigation-p... 27, 2012 – When I perform a query such as: _domainContext. Where(u ... I am not 100% sure, but I have an inkling as to what the problem is.

Stackoverflow.com/.../using-take-in-sql-linq-query-syntactic-differences-...‎CachedAug 2, 2012 – I'm new to C# and LINQ, but the latter is pretty cool. Stackoverflow.com/.../combining-linq-queries-in-linq-to-crm-causes-pro...‎CachedJan 18, 2013 – Something weird is going on. Www.codinghorror.com/blog/2010/03/compiled-or-bust.html‎CachedSimilarby Jeff AtwoodMar 19, 2010 – Anyway, Stack Overflow has dozens to hundreds of plain vanilla ... As Rico said, every single run of the Linq query is fully parsed every time it happens.

Weblogs.asp.net/.../applying-aggregates-... 15, 2008 – When Linq to SQL tries to assign null values to data types that is not defined as null, ... The above query causes exception because there are some employees that do ... Was not easy to find this answer, even on stackoverflow. We are using . Net Entity Framework 3.5 SP1.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions