c# - 用另一个更新数据表

标签 c# linq datatable

我有 2 个 C# 数据表,我也想更新的 master(datatable1) 有很多行,但第二个只包含几个唯一的行。以下是我想要实现的目标。 enter image description here

我一直在寻找一种方法来使用循环和 LINQ 来执行此操作,但似乎都无法更新 ECAD 列。

我试过了。

foreach (DataRow row in dtARIAA.Rows)
{
      foreach (DataRow row1 in dtReport.Rows)
      {
            if (row["Ser"] == row1["Ser"] )
            {
                row1["ECAD"] = row["Date"];
            }
      }
}
dtReport.AcceptChanges();

最佳答案

据我从上面的模式可以看出,您只需要编写并执行此 SQL。

Table1 是包含要更新日期的表,Table2 是包含日期的第二个表

update t1 set t1.ECAD  = [t2].[Date] from Table1 t1 
                         inner join Table2 t2  ON t2.Ser = t1.Ser

但是如果你想使用内存中已有的两个数据表,那么你可以使用

// Loop over the table with the unique Ser value and with the date to transfer
foreach (DataRow r in dtARIAA.Rows)
{
      // Get the rows in the destination table with the same "Ser" value
      DataRow[] destRows = dtReport.Select("Ser = " + r["Ser"]);

      // Now update the destination table rows with the Date value
      foreach (DataRow row1 in destRows)
          row1["ECAD"] = r["Date"];
}
dtReport.AcceptChanges();

关于c# - 用另一个更新数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51787725/

相关文章:

c# - 将 JToken 列表转换为其中一个值的列表

javascript - 数据表卡住 header

c# - 将数据表转换为 excel 2007(.xlsx)

c# - 在 C# 中向面板添加控件时出现性能问题

c# - 清除没有子元素的 xdt 元素

c# - 有什么方法可以在配置而不是代码中设置 Web 服务的命名空间?

c# - 需要帮助识别 IIS 中的细微错误……? session ?别处..?

c# - 使用 linq 搜索 Xml 文档不会返回任何内容

c# - 在现有查询中插入 Linq 查询部分

c# - DataTable不适合这种线程操作吗?