我已经建立了每日收集的报告,并且需要通过paymentType
汇总收集。我必须利用数据表来创建每日收集报告,因为还有一个日期范围选择器。摘要也将在按钮中链接。我想知道如何创建此摘要。有人可以帮我这样做吗?
答案 0 :(得分:0)
请尝试以下操作:
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Type", typeof(string));
dt.Columns.Add("Amount", typeof(decimal));
dt.Rows.Add(new object[] {"John", "A", 1.23});
dt.Rows.Add(new object[] { "Harry", "B", 4.56 });
dt.Rows.Add(new object[] { "Dick", "C", 7.89 });
dt.Rows.Add(new object[] { "Mary", "B", 6.54 });
dt.Rows.Add(new object[] { "Pat", "D", 1.23 });
dt.Rows.Add(new object[] { "Dana", "A", 8.76 });
dt.Rows.Add(new object[] { "Rob", "C", 9.65 });
var groups = dt.AsEnumerable().GroupBy(x => x.Field<string>("Type")).ToList();
DataTable summary = new DataTable();
summary.Columns.Add("Type", typeof(string));
summary.Columns.Add("Amount", typeof(decimal));
foreach (var group in groups)
{
DataRow newRow = summary.Rows.Add();
newRow["Type"] = group.Key;
newRow["Amount"] = group.Select(x => x.Field<decimal>("Amount")).Sum();
}