Reflection list <m> to datatable | reflection ilist to datatable | reflection datatable to list <m>

Source: Internet
Author: User

Returns the able set reflection list. <m>

/// <Summary> /// obtain the list based on the reflection of the able set <m> /// </Summary> /// <typeparam name = "M"> generic Entity </typeparam> // <Param name = "DT"> datatable </param> // <returns> object set </returns> Private Static list <m> setvaluerow <m> (datatable DT) where M: New () {list <m> List = new list <m> (); Type type; propertyinfo P; m; foreach (datarow row in DT. rows) {M = new M (); type = m. getType (); foreach (datacolumn Col in DT. colu MNS) {// obtain the attribute P = type of a field. getproperty (Col. columnname); // no corresponding attribute in the object if (P = NULL) continue; string coldbtype = row [col. columnname]. getType (). fullname; // If (coldbtype! = "System. dbnull ") {Switch (P. propertytype. fullname) {Case "system. int64 ": // converts data types of different databases. For example, the number (2) of Oracle should be converted to int32 instead of the default decemal p. setvalue (M, convert. toint64 (row [col. columnname]), null); break; Case "system. int32 ": P. setvalue (M, convert. toint32 (row [col. columnname]), null); break; Case "system. int16 ": P. setvalue (M, convert. toint16 (row [col. columnname]), null); break; Case "system. string ": P. setvalue (M, convert. tostring (row [col. columnname]), null); break; Case "system. decimal ": P. setvalue (M, convert. todecimal (row [col. columnname]), null); break; Case "system. datetime ": P. setvalue (M, row [col. columnname], null); break; Case "system. double ": P. setvalue (M, convert. todouble (row [col. columnname]), null); break; Case "system. boolean ": P. setvalue (M, convert. toboolean (row [col. columnname]), null); break; Case "system. byte ": P. setvalue (M, convert. tobyte (row [col. columnname]), null); break; default: P. setvalue (M, row [col. columnname], null); break ;}} list. add (m);} return list ;}
View code

Converts an ilist collection class to a datatable

/// <Summary> /// convert the ilist collection class to a datatable // </Summary> /// <Param name = "list"> set </param> // /<returns> </returns> Public static datatable ilisttodatatable (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 ;}
View code
Convert the list <m> collection class to a able
/// <Summary> /// convert the list <m> collection class to a datatable // </Summary> /// <Param name = "list"> set </Param >/// <returns> </returns> Public static datatable ilisttodatatable <m> (list <m> 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 ;}
View code
 

Reflection list <m> to datatable | reflection ilist to datatable | reflection datatable to list <m>

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.