List to datatable (reflection)

Source: Internet
Author: User
/// <Summary>
/// Convert a 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 able
/// </Summary>
/// <Typeparam name = "T"> set item type </typeparam>
/// <Param name = "list"> set </param>
/// <Returns> dataset (table) </returns>
Public static datatable todatatable <t> (ilist <t> List)
{
Return convertx. todatatable <t> (list, null );
}
/// <Summary>
/// Convert a generic collection class to a able
/// </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 <string> 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;
}

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.