我有以下代码接受DataTable并遍历属性,然后保存数据。我正在寻找一种更有效的处理方法,这样我就不必手动硬编码ItemArray计数以及行名称。任何人都可以提供有关我如何改善这一点的详细信息吗?
IList<account_dim_temp> a1 = new List<account_dim_temp>();
foreach (DataRow row in data.Rows)
{
account_dim_temp a = new account_dim_temp();
a.account_id = (string) row.ItemArray[0].ToString().Replace("\\N", "");
a.canvas_id = (string) row.ItemArray[1].ToString().Replace("\\N", "");
a.name = (string) row.ItemArray[2].ToString().Replace("\\N", "");
a.depth = (string) row.ItemArray[3].ToString().Replace("\\N", "");
a.workflow_state = (string) row.ItemArray[4].ToString().Replace("\\N", "");
a.parent_account = (string) row.ItemArray[5].ToString().Replace("\\N", "");
a.parent_account_id = (string) row.ItemArray[6].ToString().Replace("\\N", "");
a.grandparent_account = (string) row.ItemArray[7].ToString().Replace("\\N", "");
a.grandparent_account_id = (string) row.ItemArray[8].ToString().Replace("\\N", "");
a.root_account = (string) row.ItemArray[9].ToString().Replace("\\N", "");
a.root_account_id = (string) row.ItemArray[10].ToString().Replace("\\N", "");
a.subaccount1 = (string) row.ItemArray[11].ToString().Replace("\\N", "");
a.subaccount1_id = (string) row.ItemArray[12].ToString().Replace("\\N", "");
a.subaccount2 = (string) row.ItemArray[13].ToString().Replace("\\N", "");
a.subaccount2_id = (string) row.ItemArray[14].ToString().Replace("\\N", "");
a.subaccount3 = (string) row.ItemArray[15].ToString().Replace("\\N", "");
a.subaccount3_id = (string) row.ItemArray[16].ToString().Replace("\\N", "");
a.subaccount4 = (string) row.ItemArray[17].ToString().Replace("\\N", "");
a.subaccount4_id = (string) row.ItemArray[18].ToString().Replace("\\N", "");
a.subaccount5 = (string) row.ItemArray[19].ToString().Replace("\\N", "");
a.subaccount5_id = (string) row.ItemArray[20].ToString().Replace("\\N", "");
a.subaccount6 = (string) row.ItemArray[21].ToString().Replace("\\N", "");
a.subaccount6_id = (string) row.ItemArray[22].ToString().Replace("\\N", "");
a.subaccount7 = (string) row.ItemArray[23].ToString().Replace("\\N", "");
a.subaccount7_id = (string) row.ItemArray[24].ToString().Replace("\\N", "");
a.subaccount8 = (string) row.ItemArray[25].ToString().Replace("\\N", "");
a.subaccount8_id = (string) row.ItemArray[26].ToString().Replace("\\N", "");
a.subaccount9 = (string) row.ItemArray[27].ToString().Replace("\\N", "");
a.subaccount9_id = (string) row.ItemArray[28].ToString().Replace("\\N", "");
a.subaccount10 = (string) row.ItemArray[29].ToString().Replace("\\N", "");
a.subaccount10_id = (string) row.ItemArray[30].ToString().Replace("\\N", "");
a.subaccount11 = (string) row.ItemArray[31].ToString().Replace("\\N", "");
a.subaccount11_id = (string) row.ItemArray[32].ToString().Replace("\\N", "");
a.subaccount12 = (string) row.ItemArray[33].ToString().Replace("\\N", "");
a.subaccount12_id = (string) row.ItemArray[34].ToString().Replace("\\N", "");
a.subaccount13 = (string) row.ItemArray[35].ToString().Replace("\\N", "");
a.subaccount13_id = (string) row.ItemArray[36].ToString().Replace("\\N", "");
a.subaccount14 = (string) row.ItemArray[37].ToString().Replace("\\N", "");
a.subaccount14_id = (string) row.ItemArray[38].ToString().Replace("\\N", "");
a.subaccount15 = (string) row.ItemArray[39].ToString().Replace("\\N", "");
a.subaccount15_id = (string) row.ItemArray[40].ToString().Replace("\\N", "");
a.sis_source_id = (string) row.ItemArray[41].ToString().Replace("\\N", "");
a1.Add(a);
}
db.account_dim_temp.AddRange(a1);
db.SaveChanges();
答案 0 :(得分:0)
从[1]复制为建议:
// On all tables' rows
foreach (DataRow dtRow in dtTable.Rows)
{
// On all tables' columns
foreach(DataColumn dc in dtTable.Columns)
{
var field1 = dtRow[dc].ToString();
}
}
答案 1 :(得分:0)
如何在星号的循环中分配值?这可能吗?
foreach (DataRow row in data.Rows)
{
assignment_group_dim_temp a = new assignment_group_dim_temp();
foreach (DataColumn column in data.Columns)
{
string colName = column.ColumnName.ToString();
string colData = row[column].ToString();
//How can I assign the following variable?
**a.colName = colData;**
agd.Add(a);
}
}