Sum() public method

public Sum ( string columnName ) : IFunctionOrFrom
columnName string
return IFunctionOrFrom
        public void SumThrowsArgumentExceptionForNullColumnAlias()
        {
            var sqlBuilder = new SelectSqlBuilder(SqlCharacters.Empty);

            var exception = Assert.Throws<ArgumentException>(
                () => sqlBuilder.Sum("Column", null));

            Assert.Equal(ExceptionMessages.ArgumentNullOrEmpty.FormatWith("columnAlias"), exception.Message);
        }
        public void SelectWhereGroupByOrderByWithSqlCharacters()
        {
            var sqlBuilder = new SelectSqlBuilder(MsSqlCharacters.Instance, "CustomerId");

            var sqlQuery = sqlBuilder
                .Sum("Total")
                .From("Invoices")
                .Where("OrderDate").IsGreaterThan(new DateTime(2000, 1, 1))
                .GroupBy("Total")
                .OrderByDescending("OrderDate")
                .ToSqlQuery();

            Assert.Equal("SELECT [CustomerId],SUM([Total]) AS Total FROM [Invoices] WHERE ([OrderDate] > @p0) GROUP BY [Total] ORDER BY [OrderDate] DESC", sqlQuery.CommandText);

            Assert.Equal(1, sqlQuery.Arguments.Count);

            Assert.Equal(DbType.DateTime, sqlQuery.Arguments[0].DbType);
            Assert.Equal(new DateTime(2000, 1, 1), sqlQuery.Arguments[0].Value);
        }
        public void SelectWhereGroupByOrderBy()
        {
            var sqlBuilder = new SelectSqlBuilder(SqlCharacters.Empty, "CustomerId");

            var sqlQuery = sqlBuilder
                .Sum("Total")
                .From("Invoices")
                .Where("OrderDate").IsGreaterThan(new DateTime(2000, 1, 1))
                .GroupBy("Total")
                .OrderByDescending("OrderDate")
                .ToSqlQuery();

            Assert.Equal("SELECT CustomerId,SUM(Total) AS Total FROM Invoices WHERE (OrderDate > ?) GROUP BY Total ORDER BY OrderDate DESC", sqlQuery.CommandText);

            Assert.Equal(1, sqlQuery.Arguments.Count);

            Assert.Equal(DbType.DateTime, sqlQuery.Arguments[0].DbType);
            Assert.Equal(new DateTime(2000, 1, 1), sqlQuery.Arguments[0].Value);
        }
        public void SelectSumWithSqlCharacters()
        {
            var sqlBuilder = new SelectSqlBuilder(MsSqlCharacters.Instance, (string)null);

            var sqlQuery = sqlBuilder
                .Sum("CreditLimit")
                .From(typeof(Customer))
                .Where("CustomerStatusId").IsEqualTo(CustomerStatus.Active)
                .ToSqlQuery();

            Assert.Equal("SELECT SUM([CreditLimit]) AS CreditLimit FROM [Sales].[Customers] WHERE ([CustomerStatusId] = @p0)", sqlQuery.CommandText);

            Assert.Equal(1, sqlQuery.Arguments.Count);
            Assert.Equal(DbType.Int32, sqlQuery.Arguments[0].DbType);
            Assert.Equal(CustomerStatus.Active, sqlQuery.Arguments[0].Value);
        }
        public void SelectSumWithOtherColumn()
        {
            var sqlBuilder = new SelectSqlBuilder(SqlCharacters.Empty, "Id");

            var sqlQuery = sqlBuilder
                .Sum("CreditLimit")
                .From(typeof(Customer))
                .Where("CustomerStatusId").IsEqualTo(CustomerStatus.Active)
                .ToSqlQuery();

            Assert.Equal("SELECT Id,SUM(CreditLimit) AS CreditLimit FROM Sales.Customers WHERE (CustomerStatusId = ?)", sqlQuery.CommandText);

            Assert.Equal(1, sqlQuery.Arguments.Count);

            Assert.Equal(DbType.Int32, sqlQuery.Arguments[0].DbType);
            Assert.Equal(CustomerStatus.Active, sqlQuery.Arguments[0].Value);
        }
        public void SelectSumWithAlias()
        {
            var sqlBuilder = new SelectSqlBuilder(SqlCharacters.Empty, (string)null);

            var sqlQuery = sqlBuilder
                .Sum("CreditLimit", columnAlias: "SumCreditLimit")
                .From(typeof(Customer))
                .Where("CustomerStatusId").IsEqualTo(CustomerStatus.Active)
                .ToSqlQuery();

            Assert.Equal(1, sqlQuery.Arguments.Count);
            Assert.Equal(CustomerStatus.Active, sqlQuery.Arguments[0]);

            Assert.Equal("SELECT SUM(CreditLimit) AS SumCreditLimit FROM Sales.Customers WHERE (CustomerStatusId = ?)", sqlQuery.CommandText);
        }