LINQ to SQL查询导致StackOverflowException

时间:2013-10-15 18:59:17

标签: asp.net linq linq-to-sql .net-4.0 stack-overflow

以下函数中的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和{{ 1}} OrElse没有运气。

我认为我需要用更高效的方式重新编写此查询,并且非常欢迎任何有关如何执行此操作的建议。

更新

通过调试诊断工具中分析的Crash Dump捕获的调用堆栈(不确定我是否在此处粘贴了正确的内容):

Or

我必须承认它看起来像无限循环 - 这有点超出我的压力!

更新

下面的减少查询仍然导致StackOverflowException:

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) 

更新

尝试以下查询没有运气 - 认为它会更有效但仍会导致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

2 个答案:

答案 0 :(得分:1)

我很惊讶当前代码导致堆栈溢出,但即使它不是它也无法工作。目前,您正在从查询中调用本地方法:这不会起作用,因为它无法转换为SQL。

相反,我建议你在查询之前调用方法,将结果存储为局部变量 - 然后使用你在查询中调用它的变量:

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

答案 1 :(得分:0)

我的代码逐渐变硬,直到下面的工作:

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

现在的问题是应用程序有点慢!