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. 是否有任何理由在VBA中使用ENUM?
- 3. 是否有可能與wisj使用Vagrant?
- 4. 是否有可能使用android.app.Fragment與FragmentStatePagerAdapter
- 5. 檢查是否有任何輸入值
- 6. 是否有可能使用oracle表加入配置單元表?
- 7. 是否有可能與Java
- 8. 使用LINQ to看看是否有任何一個子記錄有sepcific值
- 9. 是否有可能在Windows中寫入任何內存地址
- 10. 是否有任何可能的方式將GeoFence添加到Mapbox?
- 11. LINQ的任何方法是否有效?
- 12. 是否有可能在「sql server項目」上使用linq
- 13. 是否有可能使用LINQ獲取sql連接泄漏?
- 14. 是否有可能在enum中枚舉枚舉?
- 15. 是否有可能與LINQ合併堆棧
- 16. 是否有可能使用就是hbm2ddl
- 17. 使用css3漸變功能與xhtml - 是否有可能?
- 18. 是否有可能使用高階函數與可選參數
- 19. 使用slf4j與log4j和單獨使用log4j是否有任何性能開銷?
- 20. 是否重載tabledata影響cellForRowAtIndexPath
- 21. 有沒有可能問是否有任何參數?
- 22. 是否有可能使用CONCAT_WS添加分隔只有當值不爲空
- 23. 是否有任何全功能的Brainfuck編輯器可用?
- 24. 是否有可能阻止任何應用程序在android
- 25. SQL - 是否有可能在插入值的內部有if if
- 26. 是否有任何可用於與salesforce [skypedeveloper]
- 27. 是否有可能...是否有任何教程可用..用於構建沒有Visual Studio的SharePoint插件
- 28. 是否有可能與使用activeadmin用更少的CSS
- 29. 是否有可能在現有的對象追加值 - jquery
- 30. 是否有可能得到使用JNLP
你能張貼一些示例代碼嗎? – Tvde1
如果您在數據庫表中枚舉的數字表示形式,只需將其轉換爲Enum類型。無需加入。 –