2017-05-27 3 views
0

私はEnumを持っており、DBにテーブルがあり、テーブルデータを表示して列挙します。LINQを使用してTableDataとEnum値を結合する可能性はありますか

私はどのようにLINQを使用して達成することができます。

+1

サンプルコードを投稿できますか? – Tvde1

+0

DBテーブルの列挙型の数値表現の場合は、列挙型にキャストします。結合の必要はありません。 –

答えて

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; } 


    } 

} 
関連する問題