Entity generated by DataTable

Source: Internet
Author: User

Public static BaseEntity ReceiveEntity (DataRow dr, BaseEntity entity)
{

// Declare an instance that can be converted to the nullable type
System. ComponentModel. NullableConverter nullableDateTime = new System. ComponentModel. NullableConverter (typeof (DateTime ?));
System. ComponentModel. NullableConverter nullableDecimal = new System. ComponentModel. NullableConverter (typeof (decimal ?));
If (dr = null)
Return null;

BaseEntity baseEntity = (BaseEntity) Activator. CreateInstance (entity. GetType ());
Foreach (PropertyInfo propertyInfo in entity. GetType (). GetProperties ())
{
Try
{
If (propertyInfo. name = "NewPassword" | propertyInfo. name = "DEPT_CLASS_CODE" | propertyInfo. name = "DEPT_CODE" | propertyInfo. name = "ReadOnly ")
Continue;
If (baseEntity. GetType (). GetProperty (propertyInfo. Name). PropertyType = typeof (System. DateTime ?))
{
BaseEntity. GetType (). GetProperty (propertyInfo. Name). SetValue (baseEntity, nullableDateTime. ConvertFromString (dr [propertyInfo. Name]. ToString (), null );
}
Else if (baseEntity. GetType (). GetProperty (propertyInfo. Name). PropertyType = typeof (decimal ))
{
BaseEntity. GetType (). GetProperty (propertyInfo. Name). SetValue (baseEntity, Convert. ToDecimal (dr [propertyInfo. Name]. ToString (), null );
}
Else if (baseEntity. GetType (). GetProperty (propertyInfo. Name). PropertyType = typeof (decimal ?))
{
BaseEntity. GetType (). GetProperty (propertyInfo. Name). SetValue (baseEntity, nullableDecimal. ConvertFromString (dr [propertyInfo. Name]. ToString (), null );

}
Else
{

BaseEntity. GetType (). GetProperty (propertyInfo. Name). SetValue (baseEntity, dr [propertyInfo. Name]. ToString (), null );
}
}
Catch {}
}

Return baseEntity;
}

/// <Summary>
/// Add an object to an object set
/// </Summary>
/// <Param name = "ds"> </param>
/// <Param name = "entity"> </param>
/// <Returns> </returns>
Public static List <BaseEntity> ReceiveEntity (DataSet ds, BaseEntity entity)
{
If (! (Ds! = Null & ds. Tables. Count> 0 ))
Return null;

List <BaseEntity> entityList = new List <BaseEntity> ();
Foreach (DataRow dr in ds. Tables [0]. Rows)
{
BaseEntity baseEntity = new BaseEntity ();
BaseEntity = ReceiveEntity (dr, entity );
EntityList. Add (baseEntity );
}

Return entityList;
}

Entity generated by 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.