怎么读取DataTable某一列的值?然后用c#代码判断是否有重复数据?linq还是怎么做呢?

2024-12-14 07:43:35
推荐回答(1个)
回答1:

假设你上图表格数据为
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
为最终结果
注:转换过程中要考虑空值的情况