我想构建 lambda 表达式:
Expression<Func<MyObject, bool>> predicate = PredicateBuilder.False<MyObject>();
var param = Expression.Parameter(typeof(MyObject), "f");
if (myOperator == OperateurEnum.EG)
{
var body = Expression.Equal(
Expression.PropertyOrField(param, myProperty),
Expression.Constant(myFilterValue)
);
var lambda = Expression.Lambda<Func<MyObject, bool>>(body, param);
predicate = predicate.Or(lambda);
}
else if (myOperator == OperateurEnum.CT)
{
var body = Expression.Call(
Expression.PropertyOrField(param, myProperty),
"StartsWith",
null,
Expression.Constant(myFilterValue)
);
var lambda = Expression.Lambda<Func<MyObject, bool>>(body, param);
predicate = predicate.Or(lambda);
}
else if ()
{
...
}
var myEx = Expression.Lambda<Func<MyObject, bool>>(predicate.Body, param);
Func<MyObject, bool> lambdaDelegate = myEx.Compile();
我使用 PredicateBuilder,但出现此错误:
从范围“”引用的“ProactisMvc.Web.ProactisWsServiceReference.Offre”类型的变量“f”,但未定义
怎么了 ?还有其他解决方案吗?