c# - How to create Expression<Func<TModel, TProperty>>; -
is possible create expression<func<tmodel, bool>>()
can used in different htmlhelpers (for instance in checkboxfor()
), if have model object
this htmlhelper<tmodel> htmlhelper
and name of property (through reflection).
sure:
static expression<func<tmodel,tproperty>> createexpression<tmodel,tproperty>( string propertyname) { var param = expression.parameter(typeof(tmodel), "x"); return expression.lambda<func<tmodel, tproperty>>( expression.propertyorfield(param, propertyname), param); }
then:
var lambda = createexpression<somemodel, bool>("isalive");