当我写如下lambda表达式时:
Expression<Func<string, bool>> e1 = y => y.Length < 100;
Expression<Func<string, bool>> e2 = y => y.Length < 200;
var e3 = Expression.And(e1.Body, e2.Body);
var e4 = Expression.Lambda<Func<string, bool>>(e3, e1.Parameters.ToArray());
e4.Compile(); // <--- causes run-time error
会报如题目的错误。variable '' of type '' referenced from scope '', but it is not defined
原因是两个表达式的条件并不是针对同一对应指定的。所以当使用And 或者 OR时两个表达式并列使用时会有问题,解决办法看我的另一篇文章。
有详细说明:https://lebang2020.cn/details/20090710uf5gw2.html