c编译Lambda表达式以访问作为另一个对象的属性的对象的属性throws Exception

本文关键字:属性 一个对象 对象 Exception throws Lambda 编译 表达式 访问 | 更新日期: 2023-09-27 17:59:29

下面是我之前的问题:

Lambda表达式访问对象的属性,该属性是c#中另一个对象的属性

我现在有另一个问题:

var param = Expression.Parameter(typeof(GAcordos.Models.Contratos), "x");
                    var body = Expression.Equal(Expression.PropertyOrField(Expression.PropertyOrField(param, propName[0]), columnName.ToString()), fixedItem, false, Type.GetType("GAcordos.Helpers.Comparators").GetMethod(oper, new Type[] { propType, propType }));
                    var lambda = Expression.Lambda<Func<GAcordos.Models.Contratos, bool>>(body, param);
                    contratosList = contratosList.Where(lambda).AsQueryable();

当将lambda传递给Expression.Where方法时,它不执行我给Equal Expression的替换方法,而是执行标准的Equal比较。

如果我编译lambda:

contratosList = contratosList.Where(lambda.Compile()).AsQueryable();

它抛出一个未设置为对象实例的异常对象引用稍后在视图中,具有以下堆栈跟踪:

 at lambda_method(Closure , Contratos )
 at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
 at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
 at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext()
 at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source)
 at lambda_method(Closure )
 at System.Linq.EnumerableExecutor`1.Execute()
 at System.Linq.EnumerableQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
 at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
 at MvcContrib.Pagination.LazyPagination`1.TryExecuteQuery() in C:'Users'daniel.almeida'Downloads'MVCContrib.source'src'MVCContrib'Pagination'LazyPagination.cs:line 62
 at MvcContrib.Pagination.LazyPagination`1.get_TotalItems() in C:'Users'daniel.almeida'Downloads'MVCContrib.source'src'MVCContrib'Pagination'LazyPagination.cs:line 85
 at MvcContrib.UI.Pager.Pager.ToHtmlString() in C:'Users'daniel.almeida'Downloads'MVCContrib.source'src'MVCContrib'UI'Pager'Pager.cs:line 130
 at MvcContrib.UI.Pager.Pager.ToString() in C:'Users'daniel.almeida'Downloads'MVCContrib.source'src'MVCContrib'UI'Pager'Pager.cs:line 125
 at ASP._Page_Views_Shared_Pager_cshtml.Execute() in c:'inetpub'wwwroot'Empresas'Proactivos'GAcordos'GAcordos'Views'Shared'Pager.cshtml:line 4
 at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
 at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
 at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
 at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
 at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
 at System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection)
 at System.Web.Mvc.Html.RenderPartialExtensions.RenderPartial(HtmlHelper htmlHelper, String partialViewName, Object model)
 at ASP._Page_Views_Contratos_Index_cshtml.Execute() in c:'inetpub'wwwroot'Empresas'Proactivos'GAcordos'GAcordos'Views'Contratos'Index.cshtml:line 29
 at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
 at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
 at System.Web.WebPages.StartPage.RunPage()
 at System.Web.WebPages.StartPage.ExecutePageHierarchy()
 at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
 at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
 at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
 at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
 at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
 at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
 at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)

可能出了什么问题?

c编译Lambda表达式以访问作为另一个对象的属性的对象的属性throws Exception

很难说,但如果必须猜测的话,我会说您的GetMethod调用返回null。

你能把这一行分解成一个临时变量并检查它的值吗?

Type.GetType("GAcordos.Helpers.Comparators").GetMethod(oper, new Type[] { propType, propType })

或者更好的是,制作一个小的独立的repo,这里的人可以运行。

问题不在于lambda表达式。这是在从数据库加载信息时造成的,在迭代主表的查询结果时,它在数据库中查询相关表。

将MultipleActiveResultSets=true添加到连接字符串的提供程序部分已解决此问题。参见解决方案:

已经有一个打开的DataReader与此命令关联,必须先关闭它。