DataTable and List Interchange

Source: Internet
Author: User

 PublicStaticClasslist2datatable {#region "Convert Generic List to DataTable"///<summary>///Convert a list{t} to a DataTable.///</summary>///<typeparam name= "T" ></typeparam>///<param name= "Items" ></param>///<returns></returns>PublicStatic DataTable converttodatatable<t> (This list<t>Items) {var TB =New DataTable (typeof(T). Name); propertyinfo[] Props =typeof (T). GetProperties (BindingFlags.Public |BindingFlags.Instance);foreach (PropertyInfo propInchProps) {Type T =Getcoretype (Prop. PropertyType); Tb. Columns.Add (Prop. Name, T); }foreach (T itemInchItems) {var values =NewObject[Props. Length];for (int i =0; I < props. Length; i++) {Values[i] = Props[i]. GetValue (Item,Null); } TB. Rows.Add (values); }ReturnTb }///<summary>///Determine of specified type is nullable///</summary>///<param name= "T" ></param>///<returns></returns>PublicStaticboolIsNullable (Type t) {return!t.isvaluetype | | (T.isgenerictype && t.getgenerictypedefinition () = =typeof (Nullable<>)); }///<summary>///Return underlying type if type is Nullable otherwise return the type.///</summary>///<param name= "T" ></param>///<returns></returns>public static Type getcoretype (type T) { if (t! = null && isnullable (t)) { if (!  T.isvaluetype) { return t;} else { return nullable.getunderlyingtype (t);}} else { return t;}} #endregion }                 

DataTable and List Interchange

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.