예제 #1
0
        /// <summary>
        /// Determine the sum of a column for records that match the supplied filter.
        /// </summary>
        /// <param name="tableName">The name of the table.</param>
        /// <param name="fieldName">The name of the field.</param>
        /// <param name="filter">Expression.</param>
        /// <returns>The sum of the specified column from the matching rows.</returns>
        public decimal Sum(string tableName, string fieldName, Expression filter)
        {
            if (String.IsNullOrEmpty(tableName))
            {
                throw new ArgumentNullException(nameof(tableName));
            }
            if (String.IsNullOrEmpty(fieldName))
            {
                throw new ArgumentNullException(nameof(fieldName));
            }
            DataTable result = Query(MysqlHelper.SumQuery(tableName, fieldName, _SumColumnName, filter));

            if (result != null &&
                result.Rows.Count > 0 &&
                result.Rows[0].Table.Columns.Contains(_SumColumnName) &&
                result.Rows[0][_SumColumnName] != null &&
                result.Rows[0][_SumColumnName] != DBNull.Value)
            {
                return(Convert.ToDecimal(result.Rows[0][_SumColumnName]));
            }
            return(0m);
        }