/// <summary>
/// datatable 实现GROUPBY
/// </summary>
/// <param name="p_Table"></param>
/// <returns></returns>
public DataTable GetGroupTable(DataTable p_Table)
{
DataTable _Table = new DataTable();
_Table.Columns.Add("A", typeof(System.String));
_Table.Columns.Add("B", typeof(System.String));
_Table.Columns.Add("C", typeof(System.String));
_Table.Columns.Add("D", typeof(System.Decimal));
IList<string> _List = new List<string>();
DataColumn _Column = new DataColumn("Count", typeof(System.Decimal));//定义新列,显示总和
p_Table.Columns.Add(_Column);
_Column.Expression = "convert(_D,'System.Decimal')";
foreach (DataRow _Row in p_Table.Rows)
{
object _A = _Row["A"];
object _B = _Row["B"];
object _C = _Row["C"];
object _D = p_Table.Compute("sum(D)", "A='" + _A + "' ");
string _Key = _A.ToString();
if (_List.IndexOf(_Key) == -1)
{
_Table.Rows.Add(new object[] { _A, _B, _C, _D});
_List.Add(_Key);
}
}
//p_Table.Columns.Remove("D");
return _Table;
}