3个回答
展开全部
嘿,这方法行得通,10 积分太少了,能不能加点分啊,翻了好多网页才找到的
DataTable dt = new DataTable(); //原来的资料
dt = GetGroupedBy(dt, "Name,LogonCount,LoseeCount,LeaveCount", "Name", "Sum");
private DataTable GetGroupedBy(DataTable dt, string columnNamesInDt, string groupByColumnNames, string typeOfCalculation)
{
//Return its own if the column names are empty
if (columnNamesInDt == string.Empty || groupByColumnNames == string.Empty)
{
return dt;
}
//Once the columns are added find the distinct rows and group it bu the numbet
DataTable _dt = dt.DefaultView.ToTable(true, groupByColumnNames);
//The column names in data table
string[] _columnNamesInDt = columnNamesInDt.Split(',');
for (int i = 0; i < _columnNamesInDt.Length; i = i + 1)
{
if (_columnNamesInDt[i] != groupByColumnNames)
{
_dt.Columns.Add(_columnNamesInDt[i]);
}
}
//Gets the collection and send it back
for (int i = 0; i < _dt.Rows.Count; i = i + 1)
{
for (int j = 0; j < _columnNamesInDt.Length; j = j + 1)
{
if (_columnNamesInDt[j] != groupByColumnNames)
{
_dt.Rows[i][j] = dt.Compute(typeOfCalculation + "(" + _columnNamesInDt[j] + ")", groupByColumnNames + " = '" + _dt.Rows[i][groupByColumnNames].ToString() + "'");
}
}
}
return _dt;
}
DataTable dt = new DataTable(); //原来的资料
dt = GetGroupedBy(dt, "Name,LogonCount,LoseeCount,LeaveCount", "Name", "Sum");
private DataTable GetGroupedBy(DataTable dt, string columnNamesInDt, string groupByColumnNames, string typeOfCalculation)
{
//Return its own if the column names are empty
if (columnNamesInDt == string.Empty || groupByColumnNames == string.Empty)
{
return dt;
}
//Once the columns are added find the distinct rows and group it bu the numbet
DataTable _dt = dt.DefaultView.ToTable(true, groupByColumnNames);
//The column names in data table
string[] _columnNamesInDt = columnNamesInDt.Split(',');
for (int i = 0; i < _columnNamesInDt.Length; i = i + 1)
{
if (_columnNamesInDt[i] != groupByColumnNames)
{
_dt.Columns.Add(_columnNamesInDt[i]);
}
}
//Gets the collection and send it back
for (int i = 0; i < _dt.Rows.Count; i = i + 1)
{
for (int j = 0; j < _columnNamesInDt.Length; j = j + 1)
{
if (_columnNamesInDt[j] != groupByColumnNames)
{
_dt.Rows[i][j] = dt.Compute(typeOfCalculation + "(" + _columnNamesInDt[j] + ")", groupByColumnNames + " = '" + _dt.Rows[i][groupByColumnNames].ToString() + "'");
}
}
}
return _dt;
}
推荐于2018-05-18
展开全部
DataTable.Compute("sum(LogonCount)","XX='张三'"),可以每个列的统计,想要一次性统计出你想要的结果,用sql最简单。
追问
数据不是从数据里面查出来的
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
应该是具体的sql语句来实现统计查询了
追问
数据不是从数据库里面查出来的,如果把当前数据存到数据库在用sql语句查出来的话是不是有点影响性能
追答
一定是的 或者可先取出来 而后操作也可以 的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |