/// <Summary>
/// Use the attribute name of the class to correspond to the field name in the DataTable
/// </Summary>
Public static class TableToModel
{
/// <Summary>
/// DataRow Extension Method: converts the DataRow type to an object of the specified type.
/// </Summary>
/// <Typeparam name = "T"> entity type </typeparam>
/// <Returns> </returns>
Public static T ToModel <T> (this DataRow dr) where T: class, new ()
{
Return ToModel <T> (dr, true );
}
/// <Summary>
/// Convert the DataRow type to an object of the specified type
/// </Summary>
/// <Typeparam name = "T"> entity type </typeparam>
/// <Param name = "dateTimeToString"> whether to convert a date to a string. The default value is true. </param>
/// <Returns> </returns>
/// <Summary>
Public static T ToModel <T> (this DataRow dr, bool dateTimeToString) where T: class, new ()
{
If (dr! = Null)
Return ToList <T> (dr. Table, dateTimeToString). First ();
Return null;
}
/// <Summary>
/// Convert the DataTable type to an object set of the specified type
/// </Summary>
/// <Typeparam name = "T"> entity type </typeparam>
/// <Returns> </returns>
Public static List <T> ToList <T> (this DataTable dt) where T: class, new ()
{
Return ToList <T> (dt, true );
}
/// <Summary>
/// Convert the DataTable type to an object set of the specified type
/// </Summary>
/// <Typeparam name = "T"> entity type </typeparam>
/// <Param name = "dateTimeToString"> whether to convert a date to a string. The default value is true. </param>
/// <Returns> </returns>
Public static List <T> ToList <T> (this DataTable dt, bool dateTimeToString) where T: class, new ()
{
List <T> list = new List <T> ();
If (dt! = Null)
{
List <PropertyInfo> infos = new List <PropertyInfo> ();
Array. ForEach <PropertyInfo> (typeof (T). GetProperties (), p =>
{
If (Dt. Columns. Contains (P. Name) = true)
{
Infos. Add (P );
}
});
Setlist <t> (list, Infos, DT, datetimetostring );
}
Return list;
}
# Region private Method
Private Static void setlist <t> (list <t> list, list <propertyinfo> Infos, datatable DT, bool datetimetostring) where T: Class, new ()
{
Foreach (datarow DR in DT. Rows)
{
T model = new T ();
Infos. ForEach (p =>
{
If (dr [p. Name]! = DBNull. Value)
{
Object tempValue = dr [p. Name];
If (dr [p. Name]. GetType () = typeof (DateTime) & dateTimeToString = true)
{
TempValue = dr [p. Name]. ToString ();
}
Try
{
P. SetValue (model, tempValue, null );
}
Catch {}
}
});
List. Add (model );
}
}
# Endregion
}
/// <Summary>
/// Use the Description attribute of the class attribute to correspond to the field name in the DataTable
/// </Summary>
Public static class TableToModel
{
/// <Summary>
/// Convert the DataRow type to an object of the specified type
/// </Summary>
/// <Typeparam name = "T"> entity type </typeparam>
/// <Returns> </returns>
Public static T ToModel <T> (this DataRow dr) where T: class, new ()
{
Return ToModel <T> (dr, true );
}
/// <Summary>
/// Convert the DataRow type to an object of the specified type
/// </Summary>
/// <Typeparam name = "T"> entity type </typeparam>
/// <Param name = "dateTimeToString"> whether to convert a date to a string. The default value is true. </param>
/// <Returns> </returns>
/// <Summary>
Public static T ToModel <T> (this DataRow dr, bool dateTimeToString) where T: class, new ()
{
If (dr! = Null)
Return ToList <T> (dr. Table, dateTimeToString). First ();
Return null;
}
/// <Summary>
/// Convert the DataTable type to an object set of the specified type
/// </Summary>
/// <Typeparam name = "T"> entity type </typeparam>
/// <Returns> </returns>
Public static List <T> ToList <T> (this DataTable dt) where T: class, new ()
{
Return ToList <T> (dt, true );
}
/// <Summary>
/// Convert the DataTable type to an object set of the specified type
/// </Summary>
/// <Typeparam name = "T"> entity type </typeparam>
/// <Param name = "datetimetostring"> whether to convert a date to a string. The default value is true. </param>
/// <Returns> </returns>
Public static list <t> tolist <t> (this datatable DT, bool datetimetostring) where T: Class, new ()
{
List <t> List = new list <t> ();
If (DT! = NULL)
{
List <PropertyInfo> infos = new List <PropertyInfo> ();
Array. ForEach <PropertyInfo> (typeof (T). GetProperties (), p =>
{
Foreach (Attribute att in p. GetCustomAttributes (true ))
{
System. ComponentModel. DescriptionAttribute dscript = att as System. ComponentModel. DescriptionAttribute;
If (dscript! = Null)
{
If (dt. Columns. Contains (dscript. Description) = true)
{
Infos. Add (p );
}
}
}
});
SetList <T> (list, infos, dt, dateTimeToString );
}
Return list;
}
# Region private Method
Private static void SetList <T> (List <T> list, List <PropertyInfo> infos, DataTable dt, bool dateTimeToString) where T: class, new ()
{
Foreach (DataRow dr in dt. Rows)
{
T model = new T ();
Infos. ForEach (p =>
{
Foreach (Attribute att in p. GetCustomAttributes (true ))
{
System. componentmodel. descriptionattribute dscript = ATT as system. componentmodel. descriptionattribute;
If (dscript! = NULL)
{
If (Dr [dscript. description]! = Dbnull. value)
{
Object tempValue = dr [dscript. Description];
If (dr [dscript. Description]. GetType () = typeof (DateTime) & dateTimeToString = true)
{
TempValue = dr [p. Name]. ToString ();
}
Try
{
P. SetValue (model, tempValue, null );
}
Catch {}
}
}
}
});
List. Add (model );
}
}
# Endregion
}