c#编译Lambda Expression以访问另一个对象的属性对象的属性抛出异常

时间:2011-06-17 15:22:31

标签: c# lambda compilation expression

按照我之前的问题:

Lambda expression to access a property of an object that is property of another object in 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)

可能出现什么问题?

2 个答案:

答案 0 :(得分:0)

很难说,但如果我不得不猜我会说你的GetMethod调用返回null。

您可以将此行分解为临时变量并检查其值吗?

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

或者更好的是,制作一个独立的复制品,让人们可以在这里运行。

答案 1 :(得分:0)

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

解决了将MultipleActiveResultSets = true添加到连接字符串的提供程序部分的问题。 请参阅以下解决方案:

There is already an open DataReader associated with this Command which must be closed first.

相关问题