[c#]将一个数据表格的值复制到另一个数据在 Mvc 中的表

发布时间: 2017/2/25 3:30:35
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

事实上,我有两个数据表。这里是引用的代码。其实关系持有子 (代码没有提到这里) 的字符串值。所以我分裂的数据存储在两个表的基于条件。

 if (child.Relationship.Contains("P"))
{
   var newRow = dataTable.NewRow();
   newRow["EmployeeNo"] = child.EmployeeNo;
   newRow["FirstName"] = child.FirstName;
   newRow["MiddleName"] = child.MiddleName;
   newRow["LastName"] = child.LastName;
   newRow["FullName"] = child.FullName;
   newRow["MemberIc"] = child.MemberIc;
   dataTable.Rows.Add(newRow);
   }
else
   {
     var newRow1 = dataTable1.NewRow();
     newRow1["EmployeeNo"] = child.EmployeeNo;
     newRow1["FirstName"] = child.FirstName;
     newRow1["MiddleName"] = child.MiddleName;
     newRow1["LastName"] = child.LastName;
     newRow1["FullName"] = child.FullName;     
     newRow1["MemberIC"] = child.MemberIC;         
     dataTable1.Rows.Add(newRow1);
     }

所以,只要在第二个数据表 MemberIc 为空。我需要复制从第一个数据表格 (只有 EmployeeNo 匹配的) MemberIc 基于EmployeeNo。 如何复制值?任何帮助赞赏。在此先感谢!!!

解决方法 1:

给你。

var rows = datatable.Select("EmployeeNo = " + child.EmployeeNo);

var memberIC = 0;
if(rows.length > 0)
{
    memberIC = rows[0].Field<int>("memberIC");
}

if (child.Relationship.Contains("P"))
{
   var newRow = dataTable.NewRow();
   newRow["EmployeeNo"] = child.EmployeeNo;
   newRow["FirstName"] = child.FirstName;
   newRow["MiddleName"] = child.MiddleName;
   newRow["LastName"] = child.LastName;
   newRow["FullName"] = child.FullName;
   newRow["MemberIc"] = child.MemberIc;
   dataTable.Rows.Add(newRow);
}
else
{
    var newRow1 = dataTable1.NewRow();
    newRow1["EmployeeNo"] = child.EmployeeNo;
    newRow1["FirstName"] = child.FirstName;
    newRow1["MiddleName"] = child.MiddleName;
    newRow1["LastName"] = child.LastName;
    newRow1["FullName"] = child.FullName;     
    newRow1["MemberIC"] = child.MemberIC == 0 ? memberIC : child.MemberIC;         
    dataTable1.Rows.Add(newRow1);
}

这与 C#,ADO.NET 或数据表无关。这是一个简单的逻辑的什么检查以及在何处检查。

赞助商