【发布时间】:2016-12-16 08:54:08
【问题描述】:
我需要将 C# DataTable 转换为通用集合列表
DataTable Columns Respectively
1. EmpId (this is Int DataType)
2. EmpName (this is varchar DataType)
3. EmpAddress (this is varchar DataType)
4. EmpPhone (this is varchar DataType)
5. Status (this is Boolean DataType)
6. EmpRelationKey (this is int DataType)
所以我的 DataTable 包含上述字段的值。在这里我需要将此值分配到我的列表中
我的列表变量分别
class Employee
{
protected int EmpId ;
protected string EmpName =String.Empty;
protected string EmpAddress = String.Empty;
protected string EmpPhone = String.Empty;
protected bool Status ;
protected int EmpRelationKey ;
}
Declaring List
List<Employee> Emp= new List<Employee>
所以现在我需要将 DataTable 值分配给这个列表。代码应该很专业。
这个方法我试过了
List<Employee>employees = new List<Employee>();
foreach (DataRow row in dt.Rows)
{
employees.Add(new Employee
{
EmpId = Convert.ToInt32(row["EmpId"]),
EmpName = row["EmpName"].ToString() ,
EmpAddress = row["EmpName"].ToString(),
Emphone = row["EmpPhone"].ToString(),
Status = Convert.toBoolean(row["Status"])
});
}
但我不想提及列名,有没有其他方法可以在不提及 DataTable 中的每个列名的情况下分配值
【问题讨论】:
-
反思............
-
如何使用或应用于此
-
你能帮我解决这个问题并发布代码以供参考
-
但我的列表是用户定义类型