c# - Lambda compilation throws "variable '' of type '' referenced from scope '', but it is not defined" -
when attempt compile lambda shown below, throws:
variable 'model' of type 'system.collections.generic.ienumerable`1[wheelendcatalogkendo.models.sapbasicdata]' referenced scope '', not defined
public static gridboundcolumnbuilder<tmodel> buildcolumnstring<tmodel>(this gridboundcolumnbuilder<tmodel> column, webviewpage<ienumerable<tmodel>> webviewpage, int width) tmodel : class { var modelparameter = expression.parameter(typeof(ienumerable<tmodel>), "model"); expression<func<ienumerable<tmodel>, tmodel>> firstitem = (model) => model.firstordefault(); var member = memberexpression.property(firstitem.body, column.column.member); var lambda = expression.lambda<func<ienumerable<tmodel>, string>>(member, modelparameter); var title = webviewpage.html.displaynamefor(lambda).tohtmlstring(); var header = webviewpage.html.shortlabelfor(lambda).tohtmlstring().fixupnewlinesashtml(); var compiled = lambda.compile(); //throws here "variable '...' of type '...' referenced scope '', not defined" .... }
i see several similar posts; far haven't clued me in problem code. seems supplying lambda variable (as 2nd parameter argument). have no experience authoring expression trees.
any ideas?
the problem model
parameter firstitem
expression not same modelparameter
. in expression trees, parameters not compared name, reference.
this means simplest solution reuse model
parameter firstitem
, instead of creating own parameter:
var modelparameter = firstitem.parameters.single();
with modification, code work.
Comments
Post a Comment