public virtual SqlExpression TranslateSum([NotNull] Expression expression)
        {
            Check.NotNull(expression, nameof(expression));

            if (!(expression is SqlExpression sqlExpression))
            {
                sqlExpression = Translate(expression);
            }

            if (sqlExpression == null)
            {
                throw new InvalidOperationException(CoreStrings.TranslationFailed(expression.Print()));
            }

            var inputType = sqlExpression.Type.UnwrapNullableType();

            return(inputType == typeof(float)
                ? SqlExpressionFactory.Convert(
                       SqlExpressionFactory.Function(
                           "SUM",
                           new[] { sqlExpression },
                           nullResultAllowed: true,
                           argumentsPropagateNullability: new[] { false },
                           typeof(double)),
                       inputType,
                       sqlExpression.TypeMapping)
                : (SqlExpression)SqlExpressionFactory.Function(
                       "SUM",
                       new[] { sqlExpression },
                       nullResultAllowed: true,
                       argumentsPropagateNullability: new[] { false },
                       inputType,
                       sqlExpression.TypeMapping));
        }
Ejemplo n.º 2
0
        public virtual SqlExpression TranslateAverage([NotNull] Expression expression)
        {
            Check.NotNull(expression, nameof(expression));

            if (!(expression is SqlExpression sqlExpression))
            {
                sqlExpression = Translate(expression);
            }

            if (sqlExpression == null)
            {
                throw new InvalidOperationException(CoreStrings.TranslationFailed(expression.Print()));
            }

            var inputType = sqlExpression.Type.UnwrapNullableType();

            if (inputType == typeof(int) ||
                inputType == typeof(long))
            {
                sqlExpression = SqlExpressionFactory.ApplyDefaultTypeMapping(
                    SqlExpressionFactory.Convert(sqlExpression, typeof(double)));
            }

            return(inputType == typeof(float)
                ? SqlExpressionFactory.Convert(
                       SqlExpressionFactory.Function(
                           "AVG", new[] { sqlExpression }, typeof(double)),
                       sqlExpression.Type,
                       sqlExpression.TypeMapping)
                : (SqlExpression)SqlExpressionFactory.Function(
                       "AVG", new[] { sqlExpression }, sqlExpression.Type, sqlExpression.TypeMapping));
        }