我有两个C#数据表,我希望更新的主数据(Datatable1)有很多行,但第二个只包含几个唯一的行。下面是我正在努力实现的目标。

我已经寻找了一种使用循环和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();发布于 2018-08-10 21:48:57
据我所知,根据上面的模式,您只需要编写和执行此SQL。
Table1是包含要更新的日期的表,Table2是包含日期的第二个表
update t1 set t1.ECAD = [t2].[Date] from Table1 t1
inner join Table2 t2 ON t2.Ser = t1.Ser但是,如果您想使用内存中已有的两个DataTables,那么您可以使用
// 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();https://stackoverflow.com/questions/51787725
复制相似问题