Thursday, 28 February 2013
Using Group by in DataTable with c#
namesapce testNamespace
{
public partial class testClassName : System.Web.UI
{
private void chkRepeatMtrlsQty(DataTable dtMtrlChk)
{
//Pass groupby column names here
IList<string> _groupByColumnNames = new List<string>();
_groupByColumnNames.Add("Barcode");
_groupByColumnNames.Add("MaterialNo");
_groupByColumnNames.Add("CustPartNo");
_groupByColumnNames.Add("MtrlDesc");
_groupByColumnNames.Add("NV");
_groupByColumnNames.Add("SkuUM");
_groupByColumnNames.Add("ExtUM");
//Pass Aggregate function column name and alias name here
IList<DataTableAggregateFunction> _fieldsForCalculation = new List<DataTableAggregateFunction>();
_fieldsForCalculation.Add(new DataTableAggregateFunction() { enmFunction = AggregateFunction.Sum, ColumnName = "SkuQty", OutPutColumnName = "SkuQty" });
_fieldsForCalculation.Add(new DataTableAggregateFunction() { enmFunction = AggregateFunction.Sum, ColumnName = "ExtQty", OutPutColumnName = "ExtQty" });
DataTable dtGroupedBy = GetGroupedBy(dtMtrlChk, _groupByColumnNames, _fieldsForCalculation);
}
private DataTable GetGroupedBy(DataTable _dtSource, IList<string> _groupByColumnNames, IList<DataTableAggregateFunction> _fieldsForCalculation)
{
/// <summary>
/// Group by DataTable
/// </summary>
/// <param name="_dtSource"></param>
/// <param name="_groupByColumnNames"></param>
/// <param name="_fieldsForCalculation"></param>
/// <returns></returns>
//Once the columns are added find the distinct rows and group it bu the numbet
DataTable _dtReturn = _dtSource.DefaultView.ToTable(true, _groupByColumnNames.ToArray());
//The column names in data table
foreach (DataTableAggregateFunction _calculatedField in _fieldsForCalculation)
{
_dtReturn.Columns.Add(_calculatedField.OutPutColumnName);
}
//Gets the collection and send it back
for (int i = 0; i < _dtReturn.Rows.Count; i = i + 1)
{
#region Gets the filter string
string _filterString = string.Empty;
for (int j = 0; j < _groupByColumnNames.Count; j = j + 1)
{
if (!string.IsNullOrEmpty(_dtReturn.Rows[i][_groupByColumnNames[j]].ToString()))
{
if (j > 0 && !string.IsNullOrEmpty(_filterString))
{
_filterString += " AND ";
}
if (_dtReturn.Columns[_groupByColumnNames[j]].DataType == typeof(System.Int32))
{
_filterString += _groupByColumnNames[j] + " = " + _dtReturn.Rows[i][_groupByColumnNames[j]].ToString() + "";
}
else
{
_filterString += _groupByColumnNames[j] + " = '" + _dtReturn.Rows[i][_groupByColumnNames[j]].ToString() + "'";
}
}
}
#endregion
#region Compute the aggregate command
foreach (DataTableAggregateFunction _calculatedField in _fieldsForCalculation)
{
_dtReturn.Rows[i][_calculatedField.OutPutColumnName] = _dtSource.Compute(_calculatedField.enmFunction.ToString() + "(" + _calculatedField.ColumnName + ")", _filterString);
}
#endregion
}
return _dtReturn;
}
}
//create enum function
public enum AggregateFunction
{
Sum,
Avg,
Count,
Max,
Min
}
public class DataTableAggregateFunction
{
/// <summary>
/// The function to be performed
/// </summary>
public AggregateFunction enmFunction { get; set; }
/// <summary>
/// Performed for which column
/// </summary>
public string ColumnName { get; set; }
/// <summary>
/// What should be the name after output
/// </summary>
public string OutPutColumnName { get; set; }
}
}
Labels:
.Net
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment