public static T Sum <T>(this DataTable dt, string columName)
        {
            object          result = DBNull.Value;
            OperationWorker pw     = new OperationWorker();

            foreach (DataRow row in dt.Select())
            {
                result = pw.Plus(result, row[columName]);
            }

            return((T)result);
        }
        /// <summary>
        /// group by specific Colunm, to sum up others Column value which is type of int/double/decimal etc.
        /// </summary>
        /// <param name="sourceTable"></param>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public static DataTable GroupBy(this DataTable sourceTable, string columnName)
        {
            DataTable midTable = sourceTable.Copy();

            string marker       = $"Column_{Guid.NewGuid().ToString("N")}";
            string removeMarker = Guid.NewGuid().ToString("N");

            midTable.Columns.Add(marker);
            int colIndex = midTable.Columns.IndexOf(columnName);

            foreach (var raw_row in midTable.Select())
            {
                DataRow[] resultRows = midTable.Select($"{columnName}='{raw_row[columnName]}' and {marker} is null");//
                if (resultRows.Length < 1)
                {
                    continue;
                }

                DataRow targetRow = midTable.Rows.Add();

                foreach (var row in resultRows)
                {
                    for (int i = 0; i < row.ItemArray.Length; i++)
                    {
                        object item = row.ItemArray[i];
                        if (colIndex == i)
                        {
                            targetRow[i] = item;
                            continue;
                        }

                        OperationWorker pw = new OperationWorker()
                        {
                            DbNullAction = (x, y) => { x = y; return(x); }
                        };
                        targetRow[i] = pw.Plus(targetRow[i], item);
                    }
                    row[marker] = removeMarker;
                }
                targetRow[marker] = marker;
            }
            foreach (var row in midTable.Select($"{marker}='{removeMarker}'"))
            {
                midTable.Rows.Remove(row);
            }
            midTable.Columns.Remove(marker);
            return(midTable);
        }
Exemple #3
0
        public static DataRow Max(this DataTable dt, string columnName)
        {
            int     rowCount = dt.Rows.Count;
            DataRow row      = dt.FirstOrDefault();

            if (row == null)
            {
                return(row);
            }

            OperationWorker pw = new OperationWorker();

            for (int i = 1; i < rowCount; i++)
            {
                if (pw.LeftLarger(dt.Rows[i][columnName], row[columnName]))
                {
                    row = dt.Rows[i];
                }
            }
            return(row);
        }