public static IQueryable <TResult> Select <TSource, TResult>(this IAqlModifiable <TSource> source, Expression <Func <TSource, TSource, TResult> > selector)
        {
            var newSelector = ExpressionParameterRewriter.RewriteParameters(selector, "NEW", "OLD");

            return(source.Provider.CreateQuery <TResult>(
                       Expression.Call(
                           FindExtention("SelectModification", typeof(TSource), typeof(TResult)),
                           source.Expression,
                           Expression.Quote(newSelector))));
        }
        public static Expression <T> Compose <T>(this Expression <T> first, Expression <T> second,
                                                 Func <Expression, Expression, Expression> merge)
        {
            // build parameter map (from parameters of second to parameters of first)
            var map = first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] })
                      .ToDictionary(p => p.s, p => p.f);

            // replace parameters in the second lambda expression with parameters from the first
            var secondBody = ExpressionParameterRewriter.ReplaceParameters(map, second.Body);

            // apply composition of lambda expression bodies to parameters from the first expression
            return(Expression.Lambda <T>(merge(first.Body, secondBody), first.Parameters));
        }
        internal static IAqlModifiable <TSource> InternalUpsert <TSource, TOld>(this IQueryable <TSource> source, Expression <Func <TSource, object> > searchExpression,
                                                                                Expression <Func <TSource, object> > insertExpression, Expression <Func <TSource, TOld, object> > updateExpression, Type updateType)
        {
            var newUpdateExp = ExpressionParameterRewriter.RewriteParameterAt(updateExpression, 1, "OLD");

            return(source.Provider.CreateQuery <TSource>(
                       Expression.Call(
                           FindExtention("InternalUpsert", typeof(TSource), typeof(TOld)),
                           source.Expression,
                           Expression.Quote(searchExpression),
                           Expression.Quote(insertExpression),
                           Expression.Quote(newUpdateExp),
                           Expression.Constant(updateType)
                           )) as IAqlModifiable <TSource>);
        }