将一个数据表的值复制到Mvc

时间:2017-01-16 09:33:41

标签: c# asp.net asp.net-mvc-4

实际上,我有两个数据表。这是代码供参考。实际上关系包含子字符串值(此处未提及代码)。所以我根据条件将数据分成两个表格。

 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 个答案:

答案 0 :(得分: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或datatable无关。这是检查内容和检查位置的简单逻辑。