假设你上图表格数据为
datatable
dt,列分别为aa,bb,cc,dd
var
query
=
from
p
in
dt.asenumerable()
//分组
group
p
by
new
{
t1
=
p.field
("aa").tostring().substring(0,
p.field
("aa").tostring().length
-
2),
t2
=
p.field
("bb"),
t3
=
p.field
("cc")
}
into
g
select
new
{
cola
=
g.key.t1,
colb
=
g.key.t2,
colc
=
g.key.t3,
cold
=
g.sum(c
=>
c.field
("dd"))
};
//转datatable
datatable
dtnew
=
dt.clone();
datarow
drnew;
foreach
(var
p
in
query)
{
drnew
=
dtnew.newrow();
drnew["aa"]
=
p.cola;
drnew["bb"]
=
p.colb;
drnew["cc"]
=
p.colc;
drnew["dd"]
=
p.cold;
dtnew.rows.add(drnew);
}
dtnew
为最终结果
注:转换过程中要考虑空值的情况