用Linq 更新datatable 中的一列值

Dear SQL(燊) 2021-03-27 09:33:07
大神们,如果用Linq 更新datatable 中的一列值,如下代码中Datatable 中列ver 值统一在前后加上“a”,更新后值a1,a2,a3


DataTable DT = new DataTable();
DT.TableName = "b";
DT.Columns.Add(new DataColumn("code", typeof(string)));
DT.Columns.Add(new DataColumn("ver", typeof(string)));
DT.Columns.Add(new DataColumn("name", typeof(int)));


DT.Rows.Add('1', '1', 1);//添加行
DT.Rows.Add('2', '2', 1);//添加行
DT.Rows.Add('3', '3', 1);//添加行
...全文
641 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
兔子-顾问 2021-03-27
  • 打赏
  • 举报
回复
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);

    DataTable table = new DataTable();
    table.TableName = "b";
    table.Columns.Add(new DataColumn("code", typeof(string)));
    table.Columns.Add(new DataColumn("ver", typeof(string)));
    table.Columns.Add(new DataColumn("name", typeof(int)));


    table.Rows.Add('1', '1', 1);//添加行
    table.Rows.Add('2', '2', 1);//添加行
    table.Rows.Add('3', '3', 1);//添加行

    this.dataGridView1.DataSource = table;
}

private void button1_Click(object sender, EventArgs e)
{
    DataTable table = dataGridView1.DataSource as DataTable;
    table.Rows.Cast<DataRow>().ToList().ForEach(row => row["ver"] = "a" + row["ver"].ToString());

}
sinat_29306209 2021-03-27
  • 打赏
  • 举报
回复
DT.Rows.Cast<System.Data.DataRow>().ToList().ForEach(q => q["ver"] = "a" + q["ver"].ToString());
  • 打赏
  • 举报
回复
不是一个循环就可以搞定了吗。 linq不能操作DataTable吧

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧