我认为博客文章中的代码正是您所需要的:您所要做的就是使用valueSelector.Body而不是expression.Constant()将原始参数添加到生成的表达式中:
public static expression<Func<TElement, bool>> BuildRangeexpression<TElement, TValue>( expression<Func<TElement, TValue>> valueSelector, IEnumerable<Tuple<TValue, TValue>> values){ var p = valueSelector.Parameters.Single(); var equals = values.Select( tuple => expression.AndAlso( expression.GreaterThanOrEqual( valueSelector.Body, expression.Constant(tuple.Item1)), expression.LessThanOrEqual( valueSelector.Body, expression.Constant(tuple.Item2)))); var body = equals.Aggregate(expression.OrElse); return expression.Lambda<Func<TElement, bool>>(body, p);}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)