下面函数中的 LINQ to SQL 查询在运行时导致 StackOverflowException。我需要做什么来解决这个问题?我不认为这是一个无限循环问题 - 我想我只需要减少调用堆栈?
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
我花了很长时间才找到问题的原因,并且我尝试在GetUserId
查询之外接听电话,并用 和 替换AndAlso
,And
但OrElse
没有Or
运气。
我认为我需要用更有效的方法重新编写这个查询,并且非常欢迎任何关于如何做到这一点的建议。
更新
通过调试诊断工具中分析的崩溃转储捕获的调用堆栈(不确定我是否在此处粘贴了正确的内容):
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)
我必须承认它看起来像无限循环 - 这有点超出我的atm!
更新
下面的缩减查询仍然导致 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
更新
尝试了以下查询但没有成功 - 认为它会更有效,但仍会导致 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