Invoke : DataTableToList<City>.ConvertToModel(ds.Tables[0]).ToList<City>();
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Web; namespace JsonDataInMVC.DBOperator { public static class DataTableToList<T> where T : new() { public static IList<T> ConvertToModel(DataTable dt) { // definition of a set of the IList <T> TS = new new List <T> (); T T = new new T (); String TempName = "" ; // Get Public property of this model PropertyInfo [] = the propertys t.GetType () GetProperties ();. The foreach (the DataRow Row in dt.Rows) { t = new T(); foreach (PropertyInfo pi in propertys) { TempName = pi.Name; // check the column contains DataTable IF (dt.Columns.Contains (TempName)) { // determines whether the attribute has a SET IF (! Pi.CanWrite) Continue ; Object value = Row [TempName]; IF (! = Value DBNull.Value) pi.SetValue(t, value, null); } } ts.Add(t); } return ts; } } }
Reproduced in: https: //www.cnblogs.com/zhangchenliang/p/6255255.html