Using system; using system. collections. generic; using system. LINQ; using system. text; using system. data; using system. collections; using system. reflection; namespace databletolist {class converthelper <t> where T: New () {// <summary> /// use reflection and generics /// </Summary> /// <Param name = "DT"> </param> /// <returns> </returns> Public static list <t> converttolist (datatable DT) {// define the set list <t> TS = new list <t> (); // Obtain the model type = typeof (t); // define a temporary variable string tempname = string. empty; // traverses all data rows in the datatable foreach (datarow DR in DT. rows) {T = new T (); // obtain the public attributes of this model. propertyinfo [] propertys = T. getType (). getproperties (); // traverses all attributes of the object. foreach (propertyinfo PI in propertys) {tempname = pi. name; // assign the attribute name to the Temporary Variable // check whether the datatable contains this column (column name = Object attribute name) if (DT. columns. contains (tempname) {// determines whether the property has a setter if (! Pi. canwrite) continue; // This attribute cannot be written, jump out directly // value object value = Dr [tempname]; // if it is not empty, the property if (value! = Dbnull. value) Pi. setvalue (T, value, null) ;}// Add the object to ts in the generic set. add (t) ;}return ts ;}}}
/// <Summary> /// convert the collection class to a able // </Summary> /// <Param name = "list"> set </param> /// <returns> </returns> Public static datatable todatatable (ilist List) {datatable result = new datatable (); If (list. count> 0) {propertyinfo [] propertys = list [0]. getType (). getproperties (); foreach (propertyinfo PI in propertys) {result. columns. add (Pi. name, Pi. propertytype);} For (INT I = 0; I <list. count; I + +) {Arraylist templist = new arraylist (); foreach (propertyinfo PI in propertys) {object OBJ = pi. getvalue (list [I], null); templist. add (OBJ);} object [] array = templist. toarray (); result. loaddatarow (array, true) ;}} return result ;} /// <summary> /// convert a generic collection class to a datatable // </Summary> /// <typeparam name = "T"> set item type </typeparam> /// <Param name = "list"> set </param> /// <returns> dataset (table) </returns> publi C static datatable todatatable <t> (ilist <t> List) {return convertx. todatatable <t> (list, null );} /// <summary> /// convert a generic collection class to a datatable // </Summary> /// <typeparam name = "T"> set item type </typeparam> /// <Param name = "list"> set </param> /// <Param name = "propertyname"> name of the column to be returned </param> /// <returns> dataset (table) </returns> Public static datatable todatatable <t> (ilist <t> list, Params string [] propertyname) {list <strin G> propertynamelist = new list <string> (); If (propertyname! = NULL) propertynamelist. addrange (propertyname); datatable result = new datatable (); If (list. count> 0) {propertyinfo [] propertys = list [0]. getType (). getproperties (); foreach (propertyinfo PI in propertys) {If (propertynamelist. count = 0) {result. columns. add (Pi. name, Pi. propertytype);} else {If (propertynamelist. contains (Pi. name) result. columns. add (Pi. name, Pi. propertytype) ;}}for (INT I = 0; I <list. count; I ++) {arraylist templist = new arraylist (); foreach (propertyinfo PI in propertys) {If (propertynamelist. count = 0) {object OBJ = pi. getvalue (list [I], null); templist. add (OBJ);} else {If (propertynamelist. contains (Pi. name) {object OBJ = pi. getvalue (list [I], null); templist. add (OBJ) ;}} object [] array = templist. toarray (); result. loaddatarow (array, true) ;}} return result ;}
Tlist to datatable