You can use reflection to convert a DataTable to a List<t> object: the original link http://www.jb51.net/article/67386.htm
However, this method is problematic in a DataTable where a field type is Int32: An object of type "System.Int64" cannot be converted to type "System.Int32".
You can add a sentence when you assign a value:
if (pi. GetMethod.ReturnParameter.ParameterType.Name = = "Int32")
{
Value = Convert.ToInt32 (value);
}
Pi. SetValue (t, value, NULL);
Overall code:
1 /// <summary> 2 ///Convert a DataTable to a list using reflection<T>Object3 /// </summary> 4 /// <param name= "DT" >DataTable Object</param> 5 /// <returns>List<T>Collection</returns> 6 Public StaticList<t> datatabletolist<t> (DataTable DT)whereT:class,New()7 {8 //Defining Collections9list<t> ts =NewList<t>();Ten //define a temporary variable One stringTempname =string. Empty; A //traverse all data rows in a DataTable - foreach(DataRow Drinchdt. Rows) - { theT t =NewT (); - //get the public properties of this model -propertyinfo[] Propertys =T.gettype (). GetProperties (); - //Traverse All properties of this object + foreach(PropertyInfo Piinchpropertys) - { +Tempname = Pi. Name;//Assigning a property name to a temporary variable A //Check if DataTable contains this column (column name = = Property Name of object) at if(dt. Columns.contains (tempname)) - { - //Take value - ObjectValue =Dr[tempname]; - //if not NULL, the property assigned to the object - if(Value! =DBNull.Value) in { - if(pi.) GetMethod.ReturnParameter.ParameterType.Name = ="Int32") to { +Value =Convert.ToInt32 (value); - } thePi. SetValue (t, value,NULL); * } $ }Panax Notoginseng } - //object is added to the generic collection the ts. ADD (t); + } A returnts; the}
Other types of conversions summary
1 Public StaticList<t> tolist<t> ( ThisDataTable DT)whereT:class,New()2 {3Type t=typeof(T);4propertyinfo[] Propertys =t.getproperties ();5list<t> LST =NewList<t>();6 stringTypeName =string. Empty;7 foreach(DataRow Drinchdt. Rows)8 {9T entity =NewT ();Ten foreach(PropertyInfo Piinchpropertys) One { ATypeName =Pi. Name; - if(dt. Columns.contains (typeName)) - { the if(!pi. CanWrite)Continue; - ObjectValue =Dr[typename]; - if(value = = DBNull.Value)Continue; - if(pi.) PropertyType = =typeof(string)) + { -Pi. SetValue (Entity,value. ToString (),NULL); + } A Else if(pi.) PropertyType = =typeof(int) || Pi. PropertyType = =typeof(int?)) at { -Pi. SetValue (Entity,int. Parse (value. ToString ()),NULL); - } - Else if(pi.) PropertyType = =typeof(DateTime?) || Pi. PropertyType = =typeof(DateTime)) - { -Pi. SetValue (Entity, DateTime.Parse (value). ToString ()),NULL); in } - Else if(pi.) PropertyType = =typeof(float)) to { +Pi. SetValue (Entity,float. Parse (value. ToString ()),NULL); - } the Else if(pi.) PropertyType = =typeof(Double)) * { $Pi. SetValue (Entity,Double. Parse (value. ToString ()),NULL);Panax Notoginseng } - Else the { +Pi. SetValue (Entity,value,NULL); A } the } + } - lst. ADD (entity); $ } $ returnlst; -}
A problem was encountered converting a DataTable to a List<t> object: An object of type ' System.Int64 ' could not be converted to type ' System.Int32 '.