/// <summary> /// Combine property selector expression with predicate, /// passing property from selector expression as parameter. /// </summary> /// <param name="propertySelector">property selector expression.</param> /// <param name="propertyPredicate">predicate expression taking property as parameter. </param> /// <typeparam name="T1">type of property selector source. </typeparam> /// <typeparam name="T2">type of predicate parameter. </typeparam> /// <returns>combined predicate expression with property as parameter. </returns> public static Expression <Func <T1, bool> > Compose <T1, T2>( this Expression <Func <T1, T2> > propertySelector, Expression <Func <T2, bool> > propertyPredicate) { return(ParameterToMemberExpressionRebinder.CombinePropertySelectorWithPredicate( propertySelector, propertyPredicate)); }
/// <summary> /// Pass property selector expression as parameter to lambda expression with single parameter of selected property type. /// </summary> /// <param name="propertySelector">Property selector lambda expression.</param> /// <param name="lambdaExpression">Lambda expression taking given property as parameter. </param> /// <typeparam name="TSource">>Type of source object from where property is selected.</typeparam> /// <typeparam name="TNavigate">Type of property returned by property selector and used by lambda expression. </typeparam> /// <typeparam name="TResult">Type of return value for lambda expression. </typeparam> /// <returns>Lambda expression with given property as parameter.</returns> public static Expression <Func <TSource, TResult> > NavigatePropertySelectorsToNextLambda <TSource, TNavigate, TResult>( Expression <Func <TSource, TNavigate> > propertySelector, Expression <Func <TNavigate, TResult> > lambdaExpression) { var memberExpression = propertySelector.Body as MemberExpression; if (memberExpression == null) { throw new ArgumentException("propertySelector"); } var expr = Expression.Lambda <Func <TSource, TResult> >(lambdaExpression.Body, propertySelector.Parameters); var rebinder = new ParameterToMemberExpressionRebinder(lambdaExpression.Parameters[0], memberExpression); expr = (Expression <Func <TSource, TResult> >)rebinder.Visit(expr); return(expr); }
/// <summary> /// Combine property selector expression with lambda expression function, /// passing property from selector expression as parameter. /// </summary> /// <param name="propertySelector">property selector expression.</param> /// <param name="lambdaExpression">lambda expression taking property as parameter. </param> /// <typeparam name="TSource">type of property selector source. </typeparam> /// <typeparam name="TNavigate">type of property and lambda expression parameter. </typeparam> /// <typeparam name="TResult">type lambda expression return value. </typeparam> /// <returns>combined lambda expression with property as parameter. </returns> public static Expression <Func <TSource, TResult> > Navigate <TSource, TNavigate, TResult>( this Expression <Func <TSource, TNavigate> > propertySelector, Expression <Func <TNavigate, TResult> > lambdaExpression) { return(ParameterToMemberExpressionRebinder.NavigatePropertySelectorsToNextLambda(propertySelector, lambdaExpression)); }