2013-08-08 4 views
1

테이블의 열과 함께 피벗 된 열을 반환하는 저장 프로 시저가 있습니다. 저장 프로 시저를 실행 한 후에 DataTable을 얻습니다. 이제이 DataTable을 List < 'MyClass'>로 변환하고 싶습니다. DataTable에는 알려진 일부 열 (테이블에서 오는)과 피벗 결과로 알 수없는 수의 열이 있습니다.알 수없는 열 수를 가진 DataRow에 대한 비즈니스 클래스 만들기

하나의 DataRow를 실제로 나타내는 Class를 어떻게 만듭니 까? 내가 가진 아이디어는 다음입니다 :

public class TableColumns 
{ 
     public int TableColumn1 { get;set; } 
     public string TableColumn2 { get;set; } 
     public float TableColumn1 { get;set; } 
     //additional columns if any 
} 

public class PivotColumns 
{ 
     public string ColumnName { get;set; } 
     public string Value { get;set; } 
     //additional columns if any 
} 

public class MyClass 
{ 
     public TableColumns tableColumns { get;set; } 
     public List<PivotColumns> pivotedColumns { get;set; } 

     //overload the [] operator with real implementation 
     public string this[string pivotedColumnName] { get;set; } 
} 

다음 헬퍼 클래스를 변환 할 :

public static class ConversionHelper 
{ 
     public static MyClass ConvertDataRowToMyClass(DataRow dataRow) 
     { 
       // some implementation 
     } 

     public static DataRow ConvertMyClassToDataRow(MyClass myClass) 
     { 
       // some implementation 
     } 
} 

내가 위에서 언급 한 접근 방식이 얼마나 좋은가? 아이디어/대체물을 공유하십시오.

감사합니다.

답변

1

나는 아래에 나 자신을 수행했을 것입니다.

public class TableColumns 
{ 
    public int TableColumn1 { get;set; } 
    public string TableColumn2 { get;set; } 
    public float TableColumn3 { get;set; } 
    //additional columns if any 
} 

public class PivotColumns 
{ 
    public string PivotColumn1 { get;set; } 
    public int PivotColumn2 { get;set; } 
    public float PivotColumn3 { get;set; }  
    //additional columns if any 
} 

public class MyClass : TableColumns, PivotColumns{ } 




public static class ConversionHelper 
{ 
    public static List<MyClass> ConvertDataRowToMyClass(DataTable dt) 
    { 
      // some implementation 
      List<MyClass> ltMyClass = (from dr in dataTable.AsEnumerable() 
             select new MyClass 
             { 
              TableColumn1 = dr["TableColumn1"] == DBNull.Value || dr["TableColumn1"] == null ? default(int) : dr.Field<int>("TableColumn1"), 
              PivotColumn2 = dr.Field<int>("PivotColumn2"), 
              TableColumn2 = dr.Field<string>("TableColumn2") 
             }).ToList<MyClass>(); 
    } 

    public static DataTable ConvertMyClassToDataRow(List<MyClass> lstMyClass) 
    { 
      // some implementation 
      PropertyDescriptorCollection properties = 
      TypeDescriptor.GetProperties(typeof(MyClass)); 
      DataTable table = new DataTable(); 
      foreach (PropertyDescriptor prop in properties) 
       table.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType); 
      foreach (T item in data) 
      { 
       DataRow row = table.NewRow(); 
       foreach (PropertyDescriptor prop in properties) 
         row[prop.Name] = prop.GetValue(item) ?? DBNull.Value; 
       table.Rows.Add(row); 
      } 
      return table; 
    } 
} 

목록을 데이터 변환 논리 (here)에 복사했습니다.

+0

TableColumns 및 PivotColumns에서 MyClass를 상속하는 아이디어가 좋습니다. 전환 구현에 감사드립니다. 그러나 나는 DataClow와 MyClass 사이의 변환보다 MyClass를 설정하는데 더 관심이 있습니다. – hammadmirza