c# compiled Lambda Expression to access a property of an object that is property of another object throws Exception
Following my previous question:
Lambda expression to access a property of an object that is property of another object in c#
I have now another problem:
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();
When passing the lambda to the Expression.Where method it doesn't execute the replacement method I gave to the Equal Expression, and executes the standard Equal comparison.
If I compile lambda:
contratosList = contratosList.Where(lambda.Compile()).AsQueryable();
it throws an exception Object reference not set to an instance of an object. later in the View, with the following stacktrace:
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.La开发者_高级运维zyPagination`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)
What could be wrong?
Very hard to say but if I had to guess I would say your GetMethod call is returning null.
Can you factor out this line into a temporary variable and check its value?
Type.GetType("GAcordos.Helpers.Comparators").GetMethod(oper, new Type[] { propType, propType })
Or better yet, make a little standalone repro that people here can run.
The problem was not the lambda expressions. It was caused when loading info from the dababase, it was querying the database for the related tables when iterating the result of the query for the main table.
This was solved adding MultipleActiveResultSets=true to the provider part of the connection string. See the solution for:
There is already an open DataReader associated with this Command which must be closed first.
精彩评论