c# - .net How can set the anonymous type for IEnumberable -


i need convert ienumerable dataset. need write common fnction convert ienumberable type dataset. need set anonymous type. here code

public static datatable todatatable(object alist)         {             string assemblyname  = "dataaccesslayer";             string classname = "sptblsystempreference_getlistresult";             type objtype = type.gettype(classname + "," + assemblyname);  //below line working fine... how can implement objtype in ienumberable             ienumerable<dataaccesslayer.sptblsystempreference_getlistresult> objlist1 = (ienumerable<dataaccesslayer.sptblsystempreference_getlistresult>)alist;             list<dataaccesslayer.sptblsystempreference_getlistresult> objlist = objlist1.tolist();              datatable dt = new datatable();              if (objlist[0] != null)             {                 dt.tablename = objlist[0].gettype().name;                 system.reflection.propertyinfo[] propinfo = objlist[0].gettype().getproperties();                 (int propertycount = 0; propertycount < propinfo.length; propertycount++)                 {                     dt.columns.add(propinfo[propertycount].name);                 }                 (int row = 0; row < objlist.count; row++)                 {                     datarow dr;                     dr = dt.newrow();                     (int propertycount = 0; propertycount < propinfo.length; propertycount++)                     {                          object obj = propinfo[propertycount].getvalue(objlist[row], null);                         if(obj!=null)                             dr[propertycount] =  obj;                     }                     dt.rows.add(dr);                 }             }             return dt;                     }     } 

i implemented convert ilist<t> datatable , documented on blog:

public static class listextensions {     public static datatable todatatable<t>(this ilist<t> list)     {         ilist<propertyinfo> properties = list.getpropertiesofobjectinlist();         datatable resulttable = createtable(properties);          foreach(var item in list)         {             var row = createrowfromitem<t>(resulttable, item);             resulttable.rows.add(row);         }          return resulttable;     }      private static datatable createtable(ilist<propertyinfo> properties)     {         datatable resulttable = new datatable();         foreach (var property in properties)         {             resulttable.columns.add(property.name, property.propertytype);         }         return resulttable;     }      public static ilist<propertyinfo> getpropertiesofobjectinlist<t>(this ilist<t> list)     {         return typeof(t).getproperties().tolist();     }      private static datarow createrowfromitem<t>(datatable resulttable, t item)     {         var row = resulttable.newrow();         var properties = item.gettype().getproperties().tolist();         foreach (var property in properties)         {             row[property.name] = property.getvalue(item, null);         }         return row;     } } 

this allows write code datatable yourtable = yourlist.todatatable(). can read here: generic list datatable


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -