Home Dashboard Directory Help

Stack overflow in LINQ to SQL by Juan Wajnerman


 as Fixed Help for as Fixed

Sign in
to vote
Type: Bug
ID: 355026
Opened: 7/4/2008 10:30:25 AM
Access Restriction: Public
User(s) can reproduce this bug


If a LINQ to SQL query is combined with an IQueryable created from an array or a list, the internal conversion of the expression to the SQL statement fails with a StackOverflowException. For example:

int[] ids = new [] { 1, 2, 3 };
IQueryable<int> queryableIds = ids.AsQueryable();
var query = dataContext.MyTable.Where(x => queryableIds.Contains(x.Id));
Console.WriteLine(query.ToString()); // This fails

The same code doesn't fail if the query is composed directly with the array (withou converting it to IQueryable<int>) or if the IQueryable comes from another LINQ to SQL query.
Sign in to post a comment.
Posted by Microsoft on 3/3/2009 at 2:55 PM

We have changed this behavior in .NET Framework 4.0 so that you will no longer see a StackOverflowException, but with the change you will still see a NotSupportedException. So it will still fail, but in a more manageable way.

The reason for this is that LINQ to SQL just doesn't support this scenario where Contains is being called on an IQueryable that cannot be translated to a server-side SQL Server query that can be combined with the outer query that you are executing. Basically, if it's a simple set of values (like the array before you call AsQueryable(), or another IEnumerable like List<T>), those values are typically used in the query as parameters to an IN clause or multiple OR clauses. Going beyond that with a more flexible IQueryable is just not something that it can currently handle, and we will not be adding that functionality for .NET 4.0.

LINQ to SQL Team
Posted by dustbort on 1/24/2009 at 11:25 AM
In what way was this resolved? I am experiencing this problem today.

Microsoft Visual Studio 2008
Version 9.0.30729.1 SP
Microsoft .NET Framework
Version 3.5 SP1
Posted by Microsoft on 7/7/2008 at 2:50 AM
Thanks for your feedback. We are escalating this bug to the product unit who works on that specific feature area. The team will review this issue and make a decision on whether they will fix it or not for the next release.

Thank you,
Visual Studio Product Team
Sign in to post a workaround.
Posted by dustbort on 1/24/2009 at 11:58 AM
You can get around this by using LINQ syntax:

Instead of

var institutionTypes = new int[] { 3, 4, 7, 10 }.AsQueryable(); // StackOverflowException when used in subsequent LINQ to SQL query as institutionTypes.Contains(.)


var institutionTypes =
from it in new int[] { 3, 4, 7, 10 }
select it;