/// <summary> /// Takes a lambda expression that maps the elements of the enumerable and returns an /// enumerable of the elements after they were mapped. /// </summary> /// <param name="expression">The enumerable expression.</param> /// <param name="selector">The mapping function.</param> /// <returns>The enumerable after mapping.</returns> public static EnumerableExpression Select(this EnumerableExpression expression, LambdaExpression selector) { Func <ParameterExpression, Expression> SelectorBody(Func <ParameterExpression, Expression> continuation) { Expression Inner(ParameterExpression curElem) { ParameterExpression newElemVar = Expression.Variable(selector.ReturnType, "newElem"); return(Expression.Block( new[] { newElemVar }, Expression.Assign(newElemVar, selector.Substitute(curElem)), continuation(newElemVar))); } return(Inner); } return(expression.SelectRaw(SelectorBody, selector.ReturnType)); }
/// <summary> /// Takes an enumerable of element T, and an expression from T -> bool, and filters the /// enumerable to elements where the predicate is true. /// </summary> /// <param name="expression">The enumerable to filter.</param> /// <param name="predicate">The predicate to filter on.</param> /// <returns>The filtered enumerable.</returns> public static EnumerableExpression Where(this EnumerableExpression expression, LambdaExpression predicate) { return(expression.SelectRaw( k => cur => Expression.IfThen(predicate.Substitute(cur), k(cur)), expression.ElementType)); }