Tlist to datatable

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.