c# - Creating Business Class for a DataRow with unknown number of columns -


i have stored procedure returns pivoted columns along columns tables. datatable after executing stored procedure. want convert datatable list<'myclass'>. datatable has known columns ( coming tables ) , unknown number of columns result of pivot.

how create class represents 1 datarow. idea have following:

public class tablecolumns {        public int tablecolumn1 { get;set; }        public string tablecolumn2 { get;set; }        public float tablecolumn1 { get;set; }        //additional columns if }  public class pivotcolumns {        public string columnname { get;set; }        public string value { get;set; }        //additional columns if }  public class myclass {        public tablecolumns tablecolumns { get;set; }        public list<pivotcolumns> pivotedcolumns { get;set; }         //overload [] operator real implementation        public string this[string pivotedcolumnname] { get;set; } } 

and helper class conversion:

public static class conversionhelper {        public static myclass convertdatarowtomyclass(datarow datarow)        {                // implementation        }         public static datarow convertmyclasstodatarow(myclass myclass)        {                // implementation        } } 

how approach mentioned above? please share ideas / alternates

thanks

i have done below myself.

public class tablecolumns {    public int tablecolumn1 { get;set; }    public string tablecolumn2 { get;set; }    public float tablecolumn3 { get;set; }    //additional columns if }  public class pivotcolumns {    public string pivotcolumn1 { get;set; }    public int pivotcolumn2 { get;set; }    public float pivotcolumn3 { get;set; }           //additional columns if }  public class myclass : tablecolumns, pivotcolumns{ }     public static class conversionhelper {    public static list<myclass> convertdatarowtomyclass(datatable dt)    {            // 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)    {            // 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;    } } 

i have copied list datatable conversion logic here.


Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -