I have a stored procedure which returns pivoted columns along with columns from tables. I get the DataTable after executing the stored procedure. Now i want to convert this DataTable to a List<'MyClass'>. The DataTable has some known columns ( coming from tables ) and some unknown number of columns as a result of pivot.
How do i create a Class which really represents one DataRow. The idea i have is following:
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; }
}
and then a helper class to do the conversion:
public static class ConversionHelper
{
public static MyClass ConvertDataRowToMyClass(DataRow dataRow)
{
// some implementation
}
public static DataRow ConvertMyClassToDataRow(MyClass myClass)
{
// some implementation
}
}
How good is the approach i mentioned above? Please share ideas / alternates
Thanks
I would have done the below for myself.
I have copied the list to datatable conversion logic from here.