usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Data;usingSystem.Reflection;usingYto. Weixin.model;usingSystem.Collections;namespaceYto. weixin.core{ Public classlisttodatatable { Public StaticDataTable todatatable<t> (ilist<t>list) { returnTodatatable (list,NULL); } //Convert list to DataTable Public StaticDataTable todatatable<t> (ilist<t> list,params string[] PropertyName) {List<string> propertynamelist =Newlist<string>(); if(PropertyName! =NULL) {Propertynamelist.addrange (propertyname); } DataTable result=NewDataTable (); if(list. Count >0) {propertyinfo[] propertys= list[0]. GetType (). GetProperties (); foreach(PropertyInfo Piinchpropertys) { if(Propertynamelist.count = =0) { //if (DBNull.Value.Equals (PI). PropertyType))//{ // //Pi. PropertyType = DateTime; //} Type coltype = pi. PropertyType; if (Coltype.isgenerictype && coltype.getgenerictypedefinition () = = typeof (Nullable<>)) {Coltype = coltype.getgenericarguments () [0]; } result. Columns.Add (pi. Name,coltype); //result. Columns.Add (pi. Name, Pi. PropertyType); } Else { if(Propertynamelist.contains (PI). Name) {result. Columns.Add (pi. Name, Pi. PropertyType); } } } for(inti =0; I < list. Count; i++) {ArrayList templist=NewArrayList (); foreach(PropertyInfo Piinchpropertys) { if(Propertynamelist.count = =0) { Objectobj = pi. GetValue (List[i],NULL); Templist.add (obj); } Else { if(Propertynamelist.contains (PI). Name)) {Objectobj = pi. GetValue (List[i],NULL); Templist.add (obj); } } } Object[] Array =Templist.toarray (); Result. Loaddatarow (Array,true); } } returnresult; }
Perfect solution to exclude datasets that do not support system.nullable errors