0

The LINQ to SQL query in the function below causes a StackOverflowException during run-time. What do I need to do to resolve this? I don't think it's an infinite loop problem - I think I just need to reduce the call stack?

Public Shared Function AddFilters_Current(ByVal query As IQueryable(Of UserChecklistItem)) As IQueryable(Of UserChecklistItem)

    Return From r In query Where
           ((r.UserChecklist.UserId = GetUserId() AndAlso Not r.UserChecklist.DelegationUserId.HasValue) OrElse
           (r.UserChecklist.DelegationUserId.HasValue AndAlso r.UserChecklist.DelegationUserId = GetUserId())) AndAlso
           Not r.UserChecklist.Completed AndAlso
           r.UserChecklist.DateActive <= Date.Today AndAlso
           (Not r.UserChecklist.DateInactive.HasValue OrElse r.UserChecklist.DateInactive > Date.Today)
           Order By r.UserChecklistId Ascending, r.Weight Descending, r.Id Ascending
           Select r

End Function

Public Function GetUserId() As Integer
    Dim sv As Object = HttpContext.Current.Session.Item("UserId")
    If Not TypeOf sv Is Integer Then Return Nothing
    Return DirectCast(sv, Integer)
End Function

It took me a long time to track the cause of the issue down, and I've tried taking calls to GetUserId outside of query and replacing AndAlso with And and OrElse with Or with no luck.

I think I need to re-write this query with something more efficient and any advice on how to do this is very welcome.

UPDATE

Call Stack captured via Crash Dump analysed in Debug Diagnostic Tool (not sure if I have pasted the correct thing here):

NET Call Stack

Function 
DomainNeutralILStubClass.IL_STUB_PInvoke(System.RuntimeTypeHandle, System.Runtime.CompilerServices.ObjectHandleOnStack) 
System.RuntimeTypeHandle.GetGenericTypeDefinition(System.RuntimeTypeHandle, System.Runtime.CompilerServices.ObjectHandleOnStack) 
System.RuntimeType.GetGenericTypeDefinition() 
System.Data.Linq.SqlClient.SqlTypeSystem+Sql2008Provider.From(System.Type, System.Nullable`1) 
System.Data.Linq.SqlClient.SqlTypeSystem+ProviderBase.From(System.Type) 
System.Data.Linq.SqlClient.SqlTypeSystem+ProviderBase.From(System.Object) 
System.Data.Linq.SqlClient.SqlFactory.ValueFromObject(System.Object, System.Type, Boolean, System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitConstant(System.Linq.Expressions.ConstantExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitWhere(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitWhere(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitOrderBy(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression, System.Data.Linq.SqlClient.SqlOrderType) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitThenBy(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression, System.Data.Linq.SqlClient.SqlOrderType) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitThenBy(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression, System.Data.Linq.SqlClient.SqlOrderType) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitSelect(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitWhere(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitOrderBy(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression, System.Data.Linq.SqlClient.SqlOrderType) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitThenBy(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression, System.Data.Linq.SqlClient.SqlOrderType) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitThenBy(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression, System.Data.Linq.SqlClient.SqlOrderType) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitSelect(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitWhere(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 

... cut for brevity ...

System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitThenBy(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression, System.Data.Linq.SqlClient.SqlOrderType) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitThenBy(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression, System.Data.Linq.SqlClient.SqlOrderType) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitSelect(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitWhere(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitOrderBy(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression, System.Data.Linq.SqlClient.SqlOrderType) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitThenBy(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression, System.Data.Linq.SqlClient.SqlOrderType) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitThenBy(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression, System.Data.Linq.SqlClient.SqlOrderType) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitSelect(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitWhere(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitOrderBy(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression, System.Data.Linq.SqlClient.SqlOrderType) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitThenBy(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression, System.Data.Linq.SqlClient.SqlOrderType) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitThenBy(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression, System.Data.Linq.SqlClient.SqlOrderType) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitSelect(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitWhere(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitOrderBy(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression, System.Data.Linq.SqlClient.SqlOrderType) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitThenBy(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression, System.Data.Linq.SqlClient.SqlOrderType) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitThenBy(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression, System.Data.Linq.SqlClient.SqlOrderType) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitSelect(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.VisitAggregate(System.Linq.Expressions.Expression, System.Linq.Expressions.LambdaExpression, System.Data.Linq.SqlClient.SqlNodeType, System.Type) 
System.Data.Linq.SqlClient.QueryConverter.VisitSequenceOperatorCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitMethodCall(System.Linq.Expressions.MethodCallExpression) 
System.Data.Linq.SqlClient.QueryConverter.VisitInner(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.QueryConverter.ConvertOuter(System.Linq.Expressions.Expression) 
System.Data.Linq.SqlClient.SqlProvider.BuildQuery(System.Linq.Expressions.Expression, System.Data.Linq.SqlClient.SqlNodeAnnotations) 
System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(System.Linq.Expressions.Expression) 
System.Data.Linq.DataQuery`1[[System.__Canon, mscorlib]].System.Linq.IQueryProvider.Execute(System.Linq.Expressions.Expression) 
System.Web.Query.Dynamic.DynamicQueryable.Count(System.Linq.IQueryable) 
System.Web.UI.WebControls.DynamicQueryableWrapper.Count(System.Linq.IQueryable) 
System.Web.UI.WebControls.QueryableDataSourceView.ExecutePaging(System.Linq.IQueryable, System.Web.UI.WebControls.QueryContext) 
System.Web.UI.WebControls.QueryableDataSourceView.ExecuteQuery(System.Linq.IQueryable, System.Web.UI.WebControls.QueryContext) 
System.Web.UI.WebControls.LinqDataSourceView.ExecuteSelect(System.Web.UI.DataSourceSelectArguments) 
System.Web.UI.DataSourceView.Select(System.Web.UI.DataSourceSelectArguments, System.Web.UI.DataSourceViewSelectCallback) 
System.Web.UI.WebControls.DataBoundControl.PerformSelect() 
System.Web.UI.WebControls.BaseDataBoundControl.DataBind() 
System.Web.UI.WebControls.GridView.DataBind() 
System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() 
System.Web.UI.WebControls.GridView.OnPreRender(System.EventArgs) 
System.Web.UI.Control.PreRenderRecursiveInternal() 
System.Web.UI.Control.PreRenderRecursiveInternal() 
System.Web.UI.Control.PreRenderRecursiveInternal() 
System.Web.UI.Control.PreRenderRecursiveInternal() 
System.Web.UI.Control.PreRenderRecursiveInternal() 
System.Web.UI.Control.PreRenderRecursiveInternal() 
System.Web.UI.Control.PreRenderRecursiveInternal() 
System.Web.UI.Page.ProcessRequestMain(Boolean, Boolean) 
System.Web.UI.Page.ProcessRequest(Boolean, Boolean) 
System.Web.UI.Page.ProcessRequest() 
System.Web.UI.Page.ProcessRequestWithNoAssert(System.Web.HttpContext) 
System.Web.UI.Page.ProcessRequest(System.Web.HttpContext) 
ASP.dynamicdata_pagetemplates_listdetails_aspx.ProcessRequest(System.Web.HttpContext) 
System.Web.HttpApplication+CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
System.Web.HttpApplication.ExecuteStep(IExecutionStep, Boolean ByRef) 
System.Web.HttpApplication+ApplicationStepManager.ResumeSteps(System.Exception) 
System.Web.HttpApplication.System.Web.IHttpAsyncHandler.BeginProcessRequest(System.Web.HttpContext, System.AsyncCallback, System.Object) 
System.Web.HttpRuntime.ProcessRequestInternal(System.Web.HttpWorkerRequest) 
System.Web.HttpRuntime.ProcessRequestNoDemand(System.Web.HttpWorkerRequest) 
System.Web.Hosting.ISAPIRuntime.ProcessRequest(IntPtr, Int32) 
DomainNeutralILStubClass.IL_STUB_COMtoCLR(Int32, Int32, IntPtr) 

I must admit it looks like infinite loop - bit this is a bit beyond me atm!

UPDATE

Below cut-down query is still causing a StackOverflowException:

Return From r In query Where
       r.UserChecklist.DateActive <= Date.Today AndAlso
       (Not r.UserChecklist.DateInactive.HasValue OrElse r.UserChecklist.DateInactive > Date.Today)
       Order By r.UserChecklistId Ascending, r.Weight Descending, r.Id Ascending
       Select r

UPDATE

Tried the following query with no luck - thought it would be more efficient but still causes a StackOverflowException.

Dim userId As Integer = GetUserId()
Dim today As Date = Date.Today

Return From r In query Where
       If(Not r.UserChecklist.DateInactive.HasValue, True, r.UserChecklist.DateInactive > today)
       Order By r.UserChecklistId Ascending, r.Weight Descending, r.Id Ascending
       Select r
4
  • 1
    What does the stack trace look like? Commented Oct 15, 2013 at 19:01
  • 1
    My rule of thumb is if your predicates get more then 2 or 3 deep or complicated, write it as a stored procedure instead. Commented Oct 15, 2013 at 19:01
  • @JonSkeet stack trace pasted above thanks Commented Oct 16, 2013 at 6:56
  • @asawyer You're assuming that I have permission to do that! - I do, but a solution involving stored procedures or UDFs would be tricky in above function because there is no readily available reference to the DataContext. Commented Oct 16, 2013 at 6:59

2 Answers 2

1

I'm surprised that the current code is causing a stack overflow, but even if it weren't it couldn't work. Currently you're calling your local method from within your query: that's not going to work, as it can't be converted into SQL.

Instead, I suggest you call the method before your query, storing the result as a local variable - then use that variable where you were calling it in the query:

Public Shared Function AddFilters_Current(ByVal query As IQueryable(Of UserChecklistItem)) As IQueryable(Of UserChecklistItem)

    Dim userId = GetUserId()
    Return From r In query Where
       ((r.UserChecklist.UserId = userId AndAlso Not r.UserChecklist.DelegationUserId.HasValue) OrElse
       (r.UserChecklist.DelegationUserId.HasValue AndAlso r.UserChecklist.DelegationUserId = userId)) AndAlso
       Not r.UserChecklist.Completed AndAlso
       r.UserChecklist.DateActive <= Date.Today AndAlso
       (Not r.UserChecklist.DateInactive.HasValue OrElse r.UserChecklist.DateInactive > Date.Today)
       Order By r.UserChecklistId Ascending, r.Weight Descending, r.Id Ascending
       Select r

End Function
Sign up to request clarification or add additional context in comments.

13 Comments

Thanks Jon - I am clearly not doing something correctly I will try your solution, however it can't be that alone because I do this in many places and it works. I am going to try your suggestion whilst building / testing a new WHERE clause I should be able to narrow it down further as I am going to build it up bit by bit, testing as I go.
@ChrisCannon: I'm sure you really don't do that on a LINQ to SQL query. You can do it in LINQ to Objects, but you simply can't call arbitrary methods within a LINQ to SQL query.
Hmmm I believe you, but believe me my code works everywhere else. This code is being used by a MethodExpression as part of a QueryExtender does that change anything?
I also have strict="false" explicit="true" in web.config
@ChrisCannon: I've never heard of a QueryExtender before, to be honest... but you'd have to give a specific example before I could really say. Anyway, does this change work?
|
0

I made my code progressively dumber until the below worked:

Public Shared Function AddFilters_Current(ByVal query As IQueryable(Of UserChecklistItem)) As IQueryable(Of UserChecklistItem)

    Dim l As New List(Of UserChecklistItem)

    For Each i As UserChecklistItem In query

        If ((i.UserChecklist.UserId = GetUserId() AndAlso Not i.UserChecklist.DelegationUserId.HasValue) OrElse
           (i.UserChecklist.DelegationUserId.HasValue AndAlso i.UserChecklist.DelegationUserId = GetUserId())) AndAlso
           Not i.UserChecklist.Completed AndAlso
           i.UserChecklist.DateActive <= Date.Today AndAlso
           (Not i.UserChecklist.DateInactive.HasValue OrElse i.UserChecklist.DateInactive > Date.Today) Then

            l.Add(i)

        End If

    Next

    Return From r In l.AsQueryable Order By r.UserChecklistId Ascending, r.Weight Descending, r.Id Ascending Select r

End Function

The problem now is the application is a bit slow!

3 Comments

Yes, you really don't want to do this - and note that calling AsQueryable isn't helping you at all here. One thing you could try is pulling in all the results using the same Where clause as before (after applying the changes in my answer) to build a List(Of UserChecklistItem) and apply just the ordering afterwards. Then at least you'd have the filtering done in the database.
Oh no I've just discovered what is wrong with my code - it's really embarrassing and I would have found it earlier if I had debugged it properly i.e. going further up the call stack. I just needed to use If Not IsPostBack Then ... What should I do with this mess of an SO page?
Wow, I don't understand why that would cause a stack overflow, but I'm glad it's sorted. Um, possibly vote to close this yourself and edit the question to indicate what happened?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.