/// <summary> /// Combines the sequence of expressions using the specified binary operator. /// </summary> /// <param name="expressions">A sequence of expressions to combine.</param> /// <param name="op">The <see cref="BinaryOperator" /> to use when combining.</param> /// <returns>An instance of <see cref="Expression" /> that represents the combination of all the expressions in the sequence.</returns> /// <remarks> /// When the sequence is empty, null is returned. /// When the sequence contains a single expression, that expression is returned. /// In all other cases, all expressions are combined in a chain of instances of <see cref="BinaryOperationExpression" />. /// Null values in the sequence are replaced with instances of <see cref="NullExpression" />. /// </remarks> public static Expression Combined(this IEnumerable<Expression> expressions, BinaryOperator op) { Expression result = null; foreach (Expression expression in expressions.WithConvertedNulls()) { if (result == null) { result = expression; } else { result = new BinaryOperationExpression(result, expression, op); } } return result; }