C # code for converting dataset to ilist

Source: Internet
Author: User

Dataset to ilistCode:

 

///   <Summary> ,
/// Replace dataset with a generic set
///   </Summary>
///   <Typeparam name = "T"> </typeparam>
///   <Param name = "ds"> Dataset </Param>
///   <Param name = "tableindex"> Index of the data table to be converted </Param>
///   <Returns> </returns>
Public Static Ilist <t> datasettoilist <t> (Dataset ds, Int Tableindex)
{
If (DS = Null | Ds. Tables. Count < 0 )
Return Null ;
If (Tableindex> DS. Tables. Count- 1 )
Return Null ;
If (Tableindex < 0 )
Tableindex = 0 ;
Datatable dt = Ds. Tables [tableindex];

Ilist <t> result = New List <t> ();
For ( Int J = 0 ; J <DT. Rows. Count; j ++)
{
T _ t = (t) activator. createinstance ( Typeof (T ));
Propertyinfo [] propertys = _ T. GetType (). getproperties ();
Foreach (Propertyinfo pi In Propertys)
{
For ( Int I = 0 ; I <DT. Columns. Count; I ++)
{
If (PI. Name. Equals (Dt. Columns [I]. columnname ))
{
If (Dt. Rows [J] [I]! = Dbnull. value)
Pi. setvalue (_ T, DT. Rows [J] [I], Null );
Else
Pi. setvalue (_ t, Null , Null );
Break ;
}
}
} Result. Add (_ t );
} Return Result;
}
///   <Summary>
/// Replace dataset with a generic set
/// </Summary>
///   <Typeparam name = "T"> </typeparam>
/// <Param name = "ds"> Dataset </Param>
///   <Param name = "tablename"> Name of the data table to be converted </Param>
///   <Returns> </returns>
/// Hpdv2806
Public Static Ilist <t> datasettoilist <t> (Dataset ds, String Tablename)
{
Int _ Tableindex = 0 ; If (DS = Null | Ds. Tables. Count < 0 )
Return Null ; If ( String . Isnullorempty (tablename ))
Return Null ;
For ( Int I = 0 ; I <Ds. Tables. Count; I ++)
{
If (Ds. Tables [I]. tablename. Equals (tablename ))
{
_ Tableindex = I;
Break ;
}
}
Return Datasettoilist <t> (DS, _ tableindex );
}

Personal favorites for learning !!!!

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.