C# DataTable 多列分组方式

DataTable dtGroupBy = dt_com_sub.AsEnumerable().GroupBy(r => new { BudgetSubjectID = r["BudgetSubjectID"], Subjectid = r["Subjectid"], ItemNo = r["ItemNo"] }).Select(
g =>
{
var row = dt_com_sub.NewRow();

row["BudgetSubjectID"] = g.Key.BudgetSubjectID;
row["Subjectid"] = g.Key.Subjectid;
row["ItemNo"] = g.Key.ItemNo;
row["AvailableAmt"] = g.Sum(r => (decimal)r["AvailableAmt"]);
return row;
}).CopyToDataTable();

posted @ 2021-10-21 16:40  yjge  阅读(1557)  评论(0编辑  收藏  举报