Esempio n. 1
0
        public static Expression <T> ComposeFromLambda <T>(LambdaExpression[] expressions, Func <Expression, Expression, Expression> merge)
        {
            if (expressions.Length == 0)
            {
                return(null);
            }

            var body = expressions[0].Body;

            for (var i = 1; i < expressions.Length; i++)
            {
                var map   = expressions[0].Parameters.Select((f, idx) => new { f, s = expressions[i].Parameters[idx] }).ToDictionary(p => p.s, p => (Expression)p.f);
                var iBody = ParameterRebinder.ReplaceParameters(map, expressions[i].Body);
                body = merge(body, iBody);
            }

            return(Expression.Lambda <T>(body, expressions[0].Parameters));
        }
Esempio n. 2
0
        public static Expression <T> Compose <T>(this IEnumerable <Expression <T> > expressions, Func <Expression, Expression, Expression> merge)
        {
            var expressionsEnumerated = expressions.ToArray();

            if (expressionsEnumerated.Length == 0)
            {
                return(null);
            }

            var body = expressionsEnumerated[0].Body;

            for (var i = 1; i < expressionsEnumerated.Length; i++)
            {
                var map   = expressionsEnumerated[0].Parameters.Select((f, idx) => new { f, s = expressionsEnumerated[i].Parameters[idx] }).ToDictionary(p => p.s, p => (Expression)p.f);
                var iBody = ParameterRebinder.ReplaceParameters(map, expressionsEnumerated[i].Body);
                body = merge(body, iBody);
            }

            return(Expression.Lambda <T>(body, expressionsEnumerated[0].Parameters));
        }