public static BlockExpression MergeBlocks(Expression expr1, Expression expr2) { IEnumerable <ParameterExpression> variables = new ParameterExpression[0]; IEnumerable <Expression> body = new Expression[0]; if (expr1 is BlockExpression block1) { body = body.Concat(block1.Expressions); variables = variables.Concat(block1.Variables); } else { body = body.Append(expr1); } if (expr2 is BlockExpression block2) { body = body.Concat(block2.Expressions); variables = variables.Concat(block2.Variables); } else { body = body.Append(expr2); } return(Block(variables.Distinct(), body)); }