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類型。無需加入。 –