0
A
回答
0
Here is the solution to retrieve the Enum type using the linq with datatable.
namespace StackOverflow_Ex
{
class Program
{
static void Main(string[] args)
{
DataTable dt = GetEmployeeInfo();
List<Employee> listName = dt.AsEnumerable().Select(m => new Employee()
{
Id = m.Field<int>("EmpId"),
Name = m.Field<string>("EmpName"),
EmploymentType = new EmpType { Id =m.Field<int>("EmployeeType"), EmployeeType= Enum.GetName(typeof(EmployeeType), m.Field<int>("EmployeeType")) }
}).ToList();
}
static DataTable GetEmployeeInfo()
{
DataTable dtEmp = new DataTable();
DataColumn dcId = new DataColumn("EmpId");
dcId.DataType = typeof(Int32);
DataColumn dcNm = new DataColumn("EmpName");
dcNm.DataType = typeof(string);
DataColumn dcEmpType = new DataColumn("EmployeeType");
dcEmpType.DataType = typeof(Int32);
dtEmp.Columns.Add(dcId);
dtEmp.Columns.Add(dcNm);
dtEmp.Columns.Add(dcEmpType);
DataRow dr = dtEmp.NewRow();
dr["EmpId"] = 1;
dr["EmpName"] = "Ravi";
dr["EmployeeType"] = 2;
dtEmp.Rows.Add(dr);
return dtEmp;
}
}
public enum EmployeeType
{
Permanent = 0,
Contract = 1,
PartTime = 2
}
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public EmpType EmploymentType { get; set; }
}
public class EmpType
{
public int Id { get; set; }
public string EmployeeType { get; set; }
}
}
相关问题
- 1. 是否有可能使用linq
- 2. 是否有可能像使用字典一样使用Enum值对
- 3. 是否有可能在EF5中生成带有表值的Enum?
- 4. 是否有可能使用nodejs express与任何jquery库如datatables?
- 5. 是否有可能使用datajablejs与angularjs或任何替代datatablejs
- 6. 是否有可能proxify任何功能
- 7. 是否有任何理由在VBA中使用ENUM?
- 8. 是否有可能通过linq加入收藏集
- 9. 是否有可能与wisj使用Vagrant?
- 10. 是否有可能使用android.app.Fragment与FragmentStatePagerAdapter
- 11. 是否有可能使用reCAPTCHA没有iframe和任何JavaScript?
- 12. 是否有可能上传图像只有jQuery与出使用任何PHP
- 13. 检查是否有任何输入值
- 14. 是否有可能有ENUM零个元素
- 15. 是否有可能使用更加便宜,皇帝与uWSGI
- 16. 是否有可能使用oracle表加入配置单元表?
- 17. 是否有可能与VS2010
- 18. 是否有可能与Java
- 19. 是否有可能与CSS
- 20. 是否有可能与svg.marker
- 21. 是否有任何可能使用PureRenderMixin在反应本土
- 22. 是否有任何可能的危险使用“JQuery.on()”大量
- 23. 是否可以使用宏来计算C样式的`enum`值?
- 24. 是否有任何C++库可以与WMI一起使用?
- 25. 是否有可能使任何语言可执行?
- 26. 加入查询..与if控制:是否有可能?
- 27. 使用LINQ to看看是否有任何一个子记录有sepcific值
- 28. 是否有可能在Windows中写入任何内存地址
- 29. 是否有任何可能的方式使用循环访问xml值?
- 30. LINQ的任何方法是否有效?
你能张贴一些示例代码吗? – Tvde1
如果您在数据库表中枚举的数字表示形式,只需将其转换为Enum类型。无需加入。 –